C++ C++;线性层次的可变模板参数
是否可以从可变模板参数生成线性层次结构?例如:C++ C++;线性层次的可变模板参数,c++,templates,hierarchy,variadic,C++,Templates,Hierarchy,Variadic,是否可以从可变模板参数生成线性层次结构?例如: GenLinearHierarchy<A,B,C,D,...> linHierarchy; GenLinearHierarchy层次结构; 生成一个层次结构,其中a->B->C->D->…->空(其中->符号表示继承自) 其中模板参数(模板模板…参数)具有以下签名: template <class Base, class Plate> class A; A类模板; 其中Base是层次结构中A之上的类,Plate是“最
GenLinearHierarchy<A,B,C,D,...> linHierarchy;
GenLinearHierarchy层次结构;
生成一个层次结构,其中a->B->C->D->…->空(其中->符号表示继承自)
其中模板参数(模板模板…参数)具有以下签名:
template <class Base, class Plate> class A;
A类模板;
其中Base是层次结构中A之上的类,Plate是“最派生”的类(例如D//线性层次结构
#包括
模板
D类:公共基地
{
公众:
};
模板
丙级:公共基地
{
公众:
虚火
{
静态_cast(this)->Handle();
}
};
模板
B类:公共基地
{
公众:
无效句柄()
{
可能是这样的
template<typename T, typename... Args>
class GenLinearHierarchy : public T, public GenLinearHierarchy<Args...> {};
template<typename T>
class GenLinearHierarchy<T> : public T {};
模板
类genlinearchy:public T,public genlinearchy{};
模板
类GenLinearHierarchy:public T{};
我认为你不能让Plate
成为最派生的类型,因为这意味着整个类型必须将自身作为自己的模板参数之一。这是我得到的最接近的模板参数:
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
, "");
struct Empty{};
模板
结构生成层次结构
{
头型;
};
模板
结构生成层次结构
{
尾型;
};
模板结构A:Base{};
模板结构B:Base{};
模板结构C:Base{};
模板结构D:Base{};
静态断言(std::is_same::值
, "");
编辑我想我已经实现了你的愿望。祝你阅读时好运。你只能怪你自己。:)
struct Empty{};
模板
结构genlinearchyinner
{
头型;
};
模板
结构genlinearchyinner
{
尾型;
};
模板
结构genlinearchy:genlinearchyinner::type
{};
模板结构A:Base{};
模板结构B:Base{};
模板结构C:Base{};
模板结构D:Base{};
typedef genlinearhyerarchy ABCD;
静态断言(std::是::值
, "");
我有点困惑你想要a
继承什么。你不能改变类Base
和Plate
,只能改变a
是由什么组成的,但也许这不是你的意思,它只是我困惑的一个例子。这是一个问题,一张图片能说出千言万语。或者也许吧ps你想在给定一组类的情况下解决继承层次结构…现在这很有趣。这正是我想要做的-我实际上已经尝试过了。但是,有两件事-这不完全是线性层次结构,它更多的是一个“步骤”层次结构(G->a,(G->B,(G->C…)其次,与第一点相关,模板参数不会直接从集合中的下一个模板参数继承(因此它们不能说Base::DoSomething之类的内容)。我将修改我的问题,为大家提供一个“具体”的解决方案。谢谢:)太棒了!你能想出一种方法让层次结构中较高的类调用较低的类中的函数吗?这就是为什么我希望每个元素都知道最低的元素,这样它就可以通过层次结构向上传播“事件”。不过,这是一个非常酷的解决方案@用户3513346更新。小心你的愿望。(不敢相信我在早上5点写的。)哈哈看起来很完美:)那真是太棒了Oktalist。希望我自己能想到!不知道你为什么害怕它:P
template<typename T, typename... Args>
class GenLinearHierarchy : public T, public GenLinearHierarchy<Args...> {};
template<typename T>
class GenLinearHierarchy<T> : public T {};
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
, "");
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
, "");