如何制作模板结构的向量 [英] How to Make a vector of template structs

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

问题描述

可能的重复:
c++ 模板问题

大家好:

我一直在尝试执行以下操作:

I'm stuck trying to do the following:

template <typename T>
  struct test {
  T* value;
  test(int num_of_elements) { value = new T[num_of_elements] };
  }

  std::vector<test *> fields_;

即我想制作一个指向一组具有不同值类型的测试结构的指针向量?

i.e. I want to make a vector of pointers to a set of test structs with different types for value?

我该怎么做?

谢谢

罗斯

推荐答案

你可以使用类似 Boost::any.文档中的一个示例几乎准确地说明了您的用例(至少,正如您目前所描述的那样):

You could use something like Boost::any. An example from the documentation illustrates your use case almost exactly (at least, as you've described so far):

struct property
{
    property();
    property(const std::string &, const boost::any &);

    std::string name;
    boost::any value;
};

typedef std::list<property> properties;

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

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