I have a program that is as follows. There is a base template struct X
and a partial specialisation with SFINAE.
template <typename T, typename U = void>
struct X{
X() {
std::cout << "in 1" << std::endl;
};
};
template <typename T>
struct X< T, std::enable_if_t<std::is_integral_v<T>> > {
X() {
std::cout << "in 2" << std::endl;
};
};
int main() {
X<int> x;
}
When running the program in 2
is printed.
Why is it that the second specialization is chosen over the first since both of them effectively declare a
struct X<int, void>
. What makesstd::enable_if_t<std::is_integral_v<T>>
more specialized than a default template type argument as shown in the base template?Why does the default type argument of the base template have to be the same as the type defined by the partial specialization for the partial specialization to be called and
in 2
to be printed. Why does changing tostd::enable_if_t<std::is_integral_v<T>, bool>
cause the base templatein 1
to be called?