可变模板复合遗传生成 [英] variadic template complex inheritance generation

查看:145
本文介绍了可变模板复合遗传生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用可变参数模板,尝试看看可以做什么,我发现自己想知道一些事情:(已经,有点奇怪,但是与我一起)

Playing a bit with variadic templates to try and see what could be done with them, I found myself wondering about something: (granted, it's a bit weird, but bare with me)

让我们假设我有一个类可以接受其他几个类作为模板参数,每个类都有一个嵌套类(我们称之为nested_class):

Let's suppose I have a class which can take several other classes as template parameters, each of them have a nested class (let's call it nested_class):

template<typename... Classes> class MyClass {
    class InnerClass { ... };
};

我想实现的是在MyClass内部,创建一个继承自每个参数类的类嵌套类。

what I would like to achieve is inside MyClass, create a class that inherits from each of the parameters classes nested class.

例如:

class A1 {
public:
    struct nested_class {
        do_stuff() { ... }
    };
};
class A2 {
public:
    struct nested_class {
        do_other_stuff() { ... }
    };
};

using C = MyClass<A1, A2>;

这个想法是C有它的嵌套类InnerClass继承A1 :: nested_class和A2 :: nested_class。

the idea would be that C has it's nested class InnerClass that inherits A1::nested_class and A2::nested_class.

有没有什么可以实现这样的事情?我个人不能找出一种方法,但也许这是可能的。
将继承层次结构构建器,如Alexandrescu的层次结构生成器(可在Loki中找到: http ://loki-lib.sourceforge.net/html/a00653.html )会有什么帮助吗?

Would there be anything that could achieve such a thing? I personally can't figure out a way, but maybe it is possible. Would inheritance hierarchy builders like Alexandrescu's Hierarchy Generators (which can be found in Loki: http://loki-lib.sourceforge.net/html/a00653.html) would be of any help?

事先感谢任何人有一个想法。

thanks in advance if anyone has an idea.

推荐答案

使 InnerClass > nested_class es,你应该使用 Classes :: nested_class ... 模式:

To make InnerClass derived from all nested_classes, you should use Classes::nested_class... pattern:

#include <iostream>
#include <type_traits>

template <class... Classes>
class Myclass
{
public:
    class InnerClass: public Classes::nested_class...
    {

    };
};

class A1 {
public:
    struct nested_class {
        void do_stuff() { /*...*/ }
    };
};
class A2 {
public:
    struct nested_class {
        void do_other_stuff() { /*...*/ }
    };
};

int main()
{
    using Class = Myclass<A1, A2>;

    std::cout << std::is_base_of<A1::nested_class, Class::InnerClass>::value << std::endl;
    std::cout << std::is_base_of<A2::nested_class, Class::InnerClass>::value << std::endl;

    return 0;
}

这篇关于可变模板复合遗传生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆