I am writing something about Partial template specialization
.What I want to do is that using a template is_valid
to check if the class has a member type named valid
. The source code is as below.
#include <iostream>
class A {
public:
typedef void valid;
};
class B {
public:
typedef int valid;
};
class C {
};
template <typename T, typename U = void> struct is_valid
{
const static bool value = false;
};
template <typename T> struct is_valid<T, typename T::valid>
{
const static bool value = true;
};
int main()
{
std::cout << is_valid<A>::value << std::endl;
std::cout << is_valid<B>::value << std::endl;
std::cout << is_valid<C>::value << std::endl;
return 0;
}
However, the output is
1
0
0
The last 0
is clear because C
class has no member. But why is in B
case, the output is 0?
Can anyone help me to understand what is the mechanism of Partial template specialization
here?