[英]Variadic templates and multiple inheritance in c++11
i'm trying to achieve something like this: 我正在努力实现这样的目标:
I have a templated base class which i want to inherit dynamically 我有一个模板化的基类,我想动态继承
template<typename A, typename B>
class fooBase
{
public:
fooBase(){};
~fooBase(){};
};
desired method: (something like this, not really sure how to do it) 想要的方法:(这样的东西,不确定怎么做)
template <typename... Interfaces>
class foo : public Interfaces...
{
public:
foo();
~foo();
}
and my goal is to have the foo class act like this: 我的目标是让foo类行为如下:
second method: 第二种方法:
class foo()
: public fooBase<uint8_t, float>
, public fooBase<uint16_t, bool>
, public fooBase<uint32_t, int>
// and the list could go on
{
foo();
~foo();
}
with the second method the problem is that if i instantiate an foo object, it will inherit all the time those 3 base classes, i want to make it more generally and when instantiate a foo object, give it with variadic templates the parameters for the base classes, so that i can use the foo class for other types(maybe will inherit just one base class, maybe five) 使用第二种方法的问题是,如果我实例化一个foo对象,它将一直继承这3个基类,我想更普遍地做它,并且当实例化一个foo对象时,给它带有可变参数模板的基本参数类,所以我可以将foo类用于其他类型(也许只会继承一个基类,可能只有五个)
Thank you 谢谢
example for instantiating foo 用于实例化foo的示例
foo<<uint8_t, float>, <uint16_t, bool>, <uint32_t, int>, /* and the list could go on and on */> instance
Change foo to someting similar to this: 改变foo到类似的东西:
template<typename... Interfaces>
class foo : public Interfaces... {
public:
foo(Interfaces... ifaces) : Interfaces(ifaces)... {}
};
You could try recursive variadic templates, taking 2 arguments at a time to add a derivation from fooBase. 您可以尝试递归可变参数模板,一次取2个参数来添加fooBase的派生。
It could be something like: 它可能是这样的:
template<typename A, typename B>
class fooBase
{
public:
fooBase(){};
~fooBase(){};
};
template<typename A, typename B, typename ... C>
class foo: public fooBase<A, B>, public foo<C ...> {
};
// termination version by partial specialization
template<typename A, typename B>
class foo<A, B>: public fooBase<A, B> {
};
You can then declare: 然后你可以声明:
foo<uint8_t, float, uint16_t, bool, uint32_t, int> bar;
and bar will be a subobject of fooBase<uint8_t, float>
, fooBase<uint16_t, bool>
and fooBase<uint32_t, int>
和bar将是fooBase<uint8_t, float>
, fooBase<uint16_t, bool>
和fooBase<uint32_t, int>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.