I
Imre
Please take a look at the following code:
template <typename T, typename Enable = void>
class A
{
public:
enum { value = 0 };
};
template <typename T>
class A<T, typename T::Enable>
{
public:
enum { value = 1 };
};
class B
{
public:
typedef void Enable;
};
class C
{
};
int main(int argc, char *argv[])
{
int b = A<B>::value; // should be 1
int c = A<C>::value; // should be 0
return 0;
}
The goal here was to create a class template called A in a way that
A<T>::value is 1 if T::Enable exists, and 0 otherwise. I've tried this
on MSVC++ 7.1, and it actually works, but only if T::Enable is exactly
the same type as the default value of the Enable template argument of
A. For example, if I change the typedef in B to
typedef int Enable; // int instead of void
then the value of variable b in main will be 0.
I'd like to know why's that. I thought that the specialization would be
a better match for all types where T::Enable exists, regardless of what
type it actually means. And I can't really see the connection with the
second template argument.
Imre
template <typename T, typename Enable = void>
class A
{
public:
enum { value = 0 };
};
template <typename T>
class A<T, typename T::Enable>
{
public:
enum { value = 1 };
};
class B
{
public:
typedef void Enable;
};
class C
{
};
int main(int argc, char *argv[])
{
int b = A<B>::value; // should be 1
int c = A<C>::value; // should be 0
return 0;
}
The goal here was to create a class template called A in a way that
A<T>::value is 1 if T::Enable exists, and 0 otherwise. I've tried this
on MSVC++ 7.1, and it actually works, but only if T::Enable is exactly
the same type as the default value of the Enable template argument of
A. For example, if I change the typedef in B to
typedef int Enable; // int instead of void
then the value of variable b in main will be 0.
I'd like to know why's that. I thought that the specialization would be
a better match for all types where T::Enable exists, regardless of what
type it actually means. And I can't really see the connection with the
second template argument.
Imre