I
Imre
Hi
I'd like to know what the problem is with the following code. I've
tried compiling it with two compilers (VC++ 8.0, and Comeau online
compiler), and both failed to compile it, saying that there's no B::G()
function. It seems that the primary version of the CallFHelper template
class is selected for both A and B, though I think that the
specialization should be selected for B (after all,
IntToVoid<sizeof(Dummy(&T::F))>::type _is_ void, if T == B; I checked
it with boost's STATIC_ASSERT and is_same).
So, could anyone explain me why the specialization isn't used for B?
Thanks,
Imre
Here's the code:
struct A
{
int G() { return 42; }
};
struct B
{
int F() { return 42; }
};
template <int I>
struct IntToVoid
{
typedef void type;
};
template <class T, typename Enable = void>
struct CallFHelper
{
static void F(T* t)
{
// This is here only to detect if the primary template is used
instead of the spec.
t->G();
}
};
template <typename T>
int Dummy(T);
template <class T>
struct CallFHelper<T, typename IntToVoid<sizeof(Dummy(&T::F))>::type>
{
static void F(T* t)
{
t->F();
}
};
// Call T::F() if it exists, otherwise do nothing
template <class T>
void CallF(T* t)
{
CallFHelper<T>::F(t);
}
int main(int argc, char* argv[])
{
A a;
CallF(&a);
B b;
CallF(&b);
return 0;
}
I'd like to know what the problem is with the following code. I've
tried compiling it with two compilers (VC++ 8.0, and Comeau online
compiler), and both failed to compile it, saying that there's no B::G()
function. It seems that the primary version of the CallFHelper template
class is selected for both A and B, though I think that the
specialization should be selected for B (after all,
IntToVoid<sizeof(Dummy(&T::F))>::type _is_ void, if T == B; I checked
it with boost's STATIC_ASSERT and is_same).
So, could anyone explain me why the specialization isn't used for B?
Thanks,
Imre
Here's the code:
struct A
{
int G() { return 42; }
};
struct B
{
int F() { return 42; }
};
template <int I>
struct IntToVoid
{
typedef void type;
};
template <class T, typename Enable = void>
struct CallFHelper
{
static void F(T* t)
{
// This is here only to detect if the primary template is used
instead of the spec.
t->G();
}
};
template <typename T>
int Dummy(T);
template <class T>
struct CallFHelper<T, typename IntToVoid<sizeof(Dummy(&T::F))>::type>
{
static void F(T* t)
{
t->F();
}
};
// Call T::F() if it exists, otherwise do nothing
template <class T>
void CallF(T* t)
{
CallFHelper<T>::F(t);
}
int main(int argc, char* argv[])
{
A a;
CallF(&a);
B b;
CallF(&b);
return 0;
}