tags:

views:

98

answers:

1
+4  A: 

There is indeed:

typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];

template < typename T, void (T::*)() > struct ptmf_helper {};
template< typename T > no_tag has_member_foo_helper(...);

template< typename T >
yes_tag has_member_foo_helper(ptmf_helper<T, &T::foo>* p);

template< typename T >
struct has_member_foo
{
    BOOST_STATIC_CONSTANT(bool
        , value = sizeof(has_member_foo_helper<T>(0)) == sizeof(yes_tag)
        );
};

struct my {};
struct her { void foo(); };

int main()
{
    BOOST_STATIC_ASSERT(!has_member_foo<my>::value);
    BOOST_STATIC_ASSERT(has_member_foo<her>::value);

    return 0;
} 

Copy-pasted from here.

Edit: Update the code, which is compliant AFAIK. Also note that you have to know the arguments of the return type of the method you're checking for.

Staffan
Doesn't work on VC either for some reason. Gives `error C2065: 'foo' : undeclared identifier` on `has_member_foo_helper(int, void (T::*)() = `
uj2
@uj2: I updated the code, try again! It compiles on GCC.
Staffan