views:

43

answers:

1

I suspect this is not possible under the current C++ standards but I'll ask anyway.

What I'm trying to achieve is to get the compiler to figure out during compile time if a class contains any member variable of type Base (along with its derivations).

e.g.

struct Base
{
};

struct Derived : public Base
{
};

struct Foo
{
    int x;
    Derived a;
    Derived b;
};

struct Bar
{
    int x;
};

I want something along the line of,

has_member_of_type<Base, Foo>::value

in which case would be true (one or more member vars have a base type of Base), while anything else (including fundamental types) would eval to false.

e.g.

has_member_of_type<Base, char>::value = false
has_member_of_type<Base, Bar>::value = false

Note that I'm trying to use has_member_of_type in my library to detect and use different code branch if user class has a member variable of a certain type.

A: 

No, it's not possible. You can't even access the members (unless told their names), let alone probe their types.

Tony