简体   繁体   English

线性层次结构的C ++可变参数模板参数

[英]C++ Variadic Template Parameters to Linear Hierarchy

Is it possible to generate a linear hierarchy from variadic template parameters? 是否可以从可变参数模板参数生成线性层次结构? For example: 例如:

GenLinearHierarchy<A,B,C,D,...> linHierarchy;

Generates a hierarchy where A -> B -> C -> D -> ... -> Empty (where the -> symbol stands for inherits from). 生成一个层次结构,其中A-> B-> C-> D-> ...->空(->符号代表继承)。

Where the template parameters (template template... parameters) have signatures such as: 模板参数(模板模板...参数)具有如下签名:

template <class Base, class Plate> class A;

Where Base is the class above A in the hierarchy and Plate is the 'most derived' class (for example D< E<...>, A<...> >). 其中Base是层次结构中位于A之上的类,而Plate是“最派生的”类(例如D <E <...>,A <...>>)。

So far I've been unsuccessful - starting to wonder whether I'm going in circles (I do keep running into cyclic problems). 到目前为止,我一直没有成功-开始怀疑我是否正在盘旋(我确实一直遇到周期性问题)。

Sorry about the confusion - here is a 'concrete solution' (one which I'm not too fond of): 对混乱感到抱歉-这是一个“具体解决方案”(我不太喜欢这种解决方案):

// Linear Hierarchy

#include <iostream>

template <class Base, class Floor>
class D : public Base
{
public:
};

template <class Base, class Floor>
class C : public Base
{
public:
        void Fire()
        {
                static_cast<Floor*>(this)->Handle();
        }
};

template <class Base, class Floor>
class B : public Base
{
public:
        void Handle()
        {
                std::cout << "Handled" << std::endl;
        }
};

class _FINISH {};

class _START : public B<C<D<_FINISH, _START>, _START, _START> {};

int main()
{
        typedef _START A;
        A a;
        a.Fire();

        return 0;
}

So, I'm still looking for a 'GenLinearHierarchy' class which can generate something like above, but with an instatiation like this: 因此,我仍在寻找一个'GenLinearHierarchy'类,该类可以生成类似上面的内容,但带有如下所示的信息:

GenLinearHierarchy<B,C,D> linHierarchy;

Thanks :) 谢谢 :)

I don't think you can let Plate be the most derived type, as that would mean the overall type would have to have itself as one of its own template parameters. 我认为您不能让Plate是派生程度最高的类型,因为这意味着整体类型必须将自身作为自己的模板参数之一。 This is the closest I've gotten: 这是我最近得到的:

struct Empty {};

template <template <class> class Head, template <class> class... Tail>
struct GenLinearHierarchy
{
    typedef Head<typename GenLinearHierarchy<Tail...>::type> type;
};

template <template <class> class Tail>
struct GenLinearHierarchy<Tail>
{
    typedef Tail<Empty> type;
};

template <class Base> struct A : Base {};
template <class Base> struct B : Base {};
template <class Base> struct C : Base {};
template <class Base> struct D : Base {};

static_assert(std::is_same< typename GenLinearHierarchy<A,B,C,D>::type
                          , A<B<C<D<Empty>>>>                          >::value
             , "");

EDIT I think I've got what you wished for. 编辑我想我已经有了您的期望。 Good luck trying to read it. 尝试阅读它的祝你好运。 You've only got yourself to blame. 你只能怪自己。 :) :)

struct Empty {};

template <class MostDerived,
          template <class, class> class Head,
          template <class, class> class... Tail>
struct GenLinearHierarchyInner
{
    typedef Head<typename GenLinearHierarchyInner<MostDerived,
                                                  Tail...>::type,
                 MostDerived> type;
};

template <class MostDerived,
          template <class, class> class Tail>
struct GenLinearHierarchyInner<MostDerived, Tail>
{
    typedef Tail<Empty, MostDerived> type;
};

template <template <class, class> class... Types>
struct GenLinearHierarchy : GenLinearHierarchyInner<GenLinearHierarchy<Types...>,
                                                    Types...>::type
{};

template <class Base, class> struct A : Base {};
template <class Base, class> struct B : Base {};
template <class Base, class> struct C : Base {};
template <class Base, class> struct D : Base {};

typedef GenLinearHierarchy<A,B,C,D> ABCD;

static_assert(std::is_base_of< A<B<C<D<Empty, ABCD>, ABCD>, ABCD>, ABCD>
                             , ABCD                                      >::value
             , "");

It's probably something like this... 大概是这样的...

template<typename T, typename... Args>
class GenLinearHierarchy : public T, public GenLinearHierarchy<Args...> {};

template<typename T>
class GenLinearHierarchy<T> : public T {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM