C++ 11 模板,参数包的别名

C++ 11 Templates, Alias for a parameter pack

在个人项目中,我有这样的东西:

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" class 是一种适配器,让我可以将 "Entity" 视为 int、char、float 或任何我想要的东西。 DerivedA 和 DerivedB 有不同的转换方式。 然后我有一个 class 让我像这样存储我的实体的不同视图:

template <typename... Args>
class BaseManager {
  public:
    void store(Args*... args){
        //... do things
    }
};

我有很多不同的 "Entity" class 有不同的 "Base" collections。 我希望能够将类型列表存储在别名中,例如:

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

有没有办法为任意类型列表创建别名,然后在模板参数包中使用它?

你可能想要这个:

template <typename ...P> struct parameter_pack
{
    template <template <typename...> typename T> using apply = T<P...>;
};

// Example usage:

struct A {};
struct B {};
struct C {};

template <typename...> struct S {};

using my_pack = parameter_pack<A, B, C>;

my_pack::apply<S> var; // Equivalent to `S<A, B, C> var;`.

在你的情况下,它可以这样使用:

class EntityExtra : public DerivedA<int>, DerivedA<char>, DerivedB<float>{
  public:
    using desiredBases = parameter_pack<Base<int>, Base<char>, Base<float>>;
};

// ...

EntityExtra::desiredBases::apply<BaseManager> bm;
// Creates `BaseManager<Base<int>, Base<char>, Base<float>> bm;`.