gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:04 25 4
gpt4 key购买 nike

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

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 有不同的转换方式。然后我有一个类可以让我像这样存储我的实体的不同 View :

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

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

最佳答案

你可能想要这个:

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;`.

关于C++ 11 模板,参数包的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com