Finding base class at compile time

狂风中的少年 提交于 2019-12-07 06:58:16

问题


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
}


来源:https://stackoverflow.com/questions/20798707/finding-base-class-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!