jeudi 5 mars 2015

std::enable_if using its internal type and without using it


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.




Aucun commentaire:

Enregistrer un commentaire