【发布时间】:2017-07-25 03:23:39
【问题描述】:
在一个个人项目中,我有这样的事情:
template <typename T>
class Base {
//This class is abstract.
} ;
template <typename T>
class DerivedA : public Base<T> {
//...
} ;
template <typename T>
class DerivedB : Base<T> {
//...
} ;
class Entity : public DerivedA<int>, DerivedA<char>, DerivedB<float> {
//this one inherits indirectly from Base<int>, Base<char> & Base<float>
};
“Base”类是一种适配器,可以让我将“Entity”视为 int、char、float 或任何我想要的。 DerivedA 和 DerivedB 有不同的转换方式。 然后我有一个类可以让我像这样存储我的实体的不同视图:
template <typename... Args>
class BaseManager {
public:
void store(Args*... args){
//... do things
}
};
我有很多不同的“实体”类,它们有不同的“基础”集合。 我希望能够将类型列表存储在别名中,例如:
class EntityExtra : public DerivedA<int>, DerivedA<char>, DerivedB<float>{
public:
using desiredBases = Base<int>, Base<char>, Base<float>; /* here is the problem */
};
所以我可以这样使用它:
EntityExtra ee;
BaseManager<Base<int>, Base<char>, Base<float> > bm; // <- I can use it this way
BaseManager<EntityExtra::desiredBases> bm; // <- I want to use it this way
bm.store(&ee,&ee,&ee); // The first ee will be converted to a Base<int>, the second to Base<char> and so on
有没有办法为任意类型列表创建别名,然后在模板参数包中使用它?
【问题讨论】:
-
请检查
tuple的这种用法是否相关:stackoverflow.com/questions/39242178/…
标签: c++ templates alias covariance