2

Possible Duplicate:
SFINAE to check for inherited member functions

I want to perform a compile-time-check if a specific member is existing in a class. I found a few solutions like this: Is it possible to write a template to check for a function's existence?

But they all have their own disadvantages. Is there any chance to (platform-independently) check for a member-existence, even if this member was inherited by base classes?

EDIT: It seems I have to choose between type-safety without detecting members that are inherited from base-classes (1) or no type-safety but able to detect that members (2). That's frustrating me:

struct base { int foo() { return 0; } };
struct derived : public base {};
struct other {};


template<class C>
struct has_foo
{ 
private:
    typedef char(&yes)[1];
    typedef char(&no )[2];

    struct pseudo { void foo(); };
    struct base : public C, public pseudo {};

    template<typename T, T> class check{}; 

#if SFINAE_VERSION == 1
    template<class U> static yes test(check<int (U::*)(), &U::foo>* = NULL);
    template<class>   static no  test(...);
#elif SFINAE_VERSION == 2
    template<class U> static no  test(U*, check<void (pseudo::*)(), &U::foo>* = 0); 
                      static yes test(...);
#endif
public:
#if SFINAE_VERSION == 1
    static bool const value = sizeof(test(static_cast<base*>(NULL))) == sizeof(yes);
#elif SFINAE_VERSION == 2
    static bool const value = sizeof(test<C>(NULL)) == sizeof(yes);
#endif
}; 


int main()
{
    std::cout << has_foo<base>::value << std::endl;     // true  (1) | true (2)
    std::cout << has_foo<derived>::value << std::endl;  // false (1) | true (2)
    std::cout << has_foo<other>::value << std::endl;    // false (1) | false(2)
}

EIDT2: The solution provided in the "possible duplicate" doesn't solve my problem. I need full type-safety and detection of members inherited from base-classes.

Community
  • 1
  • 1
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107

0 Answers0