user1476710
user1476710

Reputation:

List of C++ class parent types

With new C++11 standard, we have type list (in variadic template) and some compile-time methods checking whether class A is base class of B.

Now, is there any way, how to get list of base classes of a class?

Example:

class A {};
class B {};
class C {};
class AB : public A, public B {};
class Test : public AB, public C {};

template<typename ...BaseTypes>
class BaseTypeList
{
public:

    static const int size = sizeof...(BaseTypes);

    // ...
};

std::cout << "Size: " << BaseTypeList<GET_BASE_TYPES(Test)>::size
    << std::endl;

Output: Size: 2 (AB and C).

(In this example, what I'm asking for is implementation of GET_BASE_TYPES(...).)

Notes:

Upvotes: 0

Views: 152

Answers (1)

quantdev
quantdev

Reputation: 23813

If you are using gcc, it implements std::tr2::direct_bases from the TR2 (see here). I dont know if it is part of C++14 or 17 yet, but it does what you want.

As stated in the code comment, it Enumerate all the direct base classes of a class. Form of a typelist.

Upvotes: 2

Related Questions