Possible Duplicate:
Possible for C++ template to check for a function’s existence?
I am trying to determine wether a type has a certain member. This is what i tried:
template <typename T,typename U=void>
class HasX
{
public:
static const bool Result=false;
};
template <typename T>
class HasX<T,typename enable_if_c<(sizeof(&T::X)>0)>::type>
{
public:
static const bool Result=true;
};
struct A
{
int X();
};
struct B
{
int Y();
};
int main()
{
cout<<HasX<A>::Result<<endl; // 1
cout<<HasX<B>::Result<<endl; // 0
}
It actually compiles and works on GCC, but VC gives error C2070: 'overloaded-function': illegal sizeof operand
at the point of instanciation.
Is there something wrong with the code, and are there other ways to do this?