向量的typedefs [英] Vector of typedefs

查看:163
本文介绍了向量的typedefs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以任何方式在 C ++ 11/14 中使用类型(def)的向量?

Is it possible in ANY way to have a vector of type(def)s in C++11/14 ?

我尝试的第一件事是有一个基类的向量,并以某种方式从它的派生形式获得typedef,但我不能得到这个工作,我尝试(不可能很可能)。

The first thing I tried was have a vector of a base class and somehow get the typedef from it's derived form but I can't get this to work whatever I try (not possible most likely).

伪C ++:

class base
{
   /* somehow access 'type' from derived */
}

template <typename T>
class derived : base
{
   typedef T type;
}

vector<base*> vec;
vec.push_back( new derived<int> );
vec.push_back( new derived<double> );
vec.push_back( new derived<float> );
vec.push_back( new derived<string> );

for(auto& item : vec)
   static_cast< item->type >( /* something */ );


推荐答案

Boost MPL为此提供了一个编译时构造,for例如:

Boost MPL provides a compile time construct for this, for example:

typedef boost::mpl::vector<int, double, std::string, CustomA, CustomB> seq_of_types;

您可以使用mpl中定义的大量元函数在编译类型与此交互。还有一些运行时交叉函数。这里的重点是,这是一个类型序列,没有每个类型的实例交互。即使运行时函数只允许与类型交互。

You can interact with this at compile type using the extensive set of meta functions defined in mpl. There are also some run-time crossover functions too. The important point here is that this is a sequence of types, there are no instances of each type to interact with. Even the runtime functions only allow interacting with types.

Boost Fusion(和 std :: tuple )步骤在这里提供一个运行时异构容器,例如

Boost Fusion (and std::tuple) steps in here to provide a runtime heterogenous container, so for example

boost::fusion::vector<int, double, std::string> v{10, 100., "Foo"};

现在在编译时,您可以访问每个条目的类型信息,

Now at compile time, you have access to the type information of each entry, and at runtime you have an instance of each type in the sequence to work with.

这是可能的,你正在尝试实现可以​​通过简单的继承,而不必诉诸于上面,所以向量包含一个指向基类的指针,它有一个虚函数,它在你想要的派生类中被覆盖。这可能是最干净的。

It could be possible that what you are trying to achieve could be done with plain inheritance without having to resort to the above, so the vector holds a pointer to base class, which has a virtual function which is overriden in the derived classes which does what you want. This is possibly the cleanest.

或者,如果使用可变参数类型,如果不使用继承, c $ c> boost :: variant ,例如:

Alternatively, the same is possible, without resorting to using inheritance if you use a variadic type such as boost::variant, for example:

std::vector<boost::variant<int, double, std::string>> entries;

现在每个条目都是 int double std :: string 。然后在迭代时,可以使用静态访问者对特定实例进行操作。

Now each entry is one of the types of int, double, std::string. Then as you iterate, you can use a static visitor to operate on the specific instance. I think I've answered a question on SO a while ago which demonstrates this.

这是什么?

编辑:根据你最后的评论,然后后者(变体)不真的飞,也不是简单的继承。我认为一个融合矢量是没有必要的,因为你不需要每个类型的实例。最适合你的是 mpl :: vector ,并使用运行时函数 mpl :: for_each

base on your last comment, then the latter (variant) doesn't really fly, and nor does plain inheritance. I think a fusion vector is really not necessary either as you don't need an instance of each type. The most suitable thing for you then is the mpl::vector, and use the runtime function mpl::for_each

这篇关于向量的typedefs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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