Finding base class at compile time

2020-03-24 03:06发布

问题:

The title almost says everything: Is there a way in C++ to get a class's base type(s) at compile time? I. e. is it possible to hand a class to a template, and let the template use other templates to which it hands the bases of the given class?

My question is not whether I can implement such a functionality myself, there is no question I can (using traits and the like). My question is whether there is some (obscure) builtin functionality that could be used to this end.

回答1:

gcc supports this. See

  • Kerrek's answer
  • tr2/type_traits
  • Andy Prowl's code example
  • n2965
  • What is the status of N2965 - std::bases and std::direct_bases?
  • How to query for all base classes of a class at compile time?

n2965 provides an example.

This simple examples illustrates the results of these type traits. In the Suppose we have the following class hierarchy:

class E {};
class D {};
class C : virtual public D, private E {};
class B : virtual public D, public E {};
class A : public B, public C {};

It follows that bases<A>::type is tuple<D, B, E, C, E>

Similarly, direct_bases<A>::type is tuple<B, C>

Andy Prowl's code example is as follows:

#include <tr2/type_traits>
#include <tuple>

template<typename T>
struct dbc_as_tuple { };

template<typename... Ts>
struct dbc_as_tuple<std::tr2::__reflection_typelist<Ts...>>
{
    typedef std::tuple<Ts...> type;
};

struct A {};
struct B {};
struct C : A, B {};

int main()
{
    using namespace std;

    using direct_base_classes = dbc_as_tuple<tr2::direct_bases<C>::type>::type;

    using first = tuple_element<0, direct_base_classes>::type;
    using second = tuple_element<1, direct_base_classes>::type;

    static_assert(is_same<first, A>::value, "Error!");   // Will not fire
    static_assert(is_same<second, B>::value, "Error!");  // Will not fire
}