SFINAE AND enable_if<>
Before C++17, there were already ways to enable or disable templates as a whole – a compile-time if
, partial specialization, SFINAE, and enable_if<>
. Referring to our previous example, is_prime<>
, where we determined the authenticity of a prime number, we can use partial specialization to choose at compile time between different type implementations. We can choose different implementations depending on the argument (we used a struct in the code because function templates do not support partial specialization):
template <std::size_t n, bool = IsPrime(n)>class hash_table; template <std::size_t n> class hash_table<n, true> { public: Â Â Â Â std::array<int, n> bucket; }; template <std::size_t n> class hash_table<n, false> { public: Â Â Â Â array<int, next_prime<n>::value> bucket; // next_prime<size_t n> is a meta function which compute // the next prime...