Why does it print "B"
#include <iostream>
template<typename T, typename U = void>
struct Test
{ static void apply() { std::cout << "A" << std::endl; } };
template<typename T>
struct Test<T, typename std::enable_if<true>::type>
{ static void apply() { std::cout << "B" << std::endl; } };
int main()
{
Test<int>::apply();
}
but this one "A"?:
#include <iostream>
template<typename T, typename U = void>
struct Test
{ static void apply() { std::cout << "A" << std::endl; } };
template<typename T>
struct Test<T, std::enable_if<true>>
{ static void apply() { std::cout << "B" << std::endl; } };
int main()
{
Test<int>::apply();
}
The only difference between them is that, in the first one, I'm using typename std::enable_it<true>::type
as U
(e.g, void
), but in the second one, I'm directly using std::enable_if<true>
as U
, which is also a well defined type and with no more meaning as void
.
Because when you write Test<int>
you are actually writing Test<int, void>
which will never be the same as Test<int, std::enable_if<true>>
(though it is the same as Test<int, typename std::enable_if<true>::type>
)