c++ - 如何获得 af boost::variant 的第 i 个基础类型(通过 const 整数)

标签 c++ templates typedef boost-variant

我想获取变体的基础类型。例如“类似这样的东西:

typedef boost::variant< shared_ptr<int> , shared_ptr<float> > VType;

typedef VType::get<0>::type  TYPE1;       // TYPE1 = shared_ptr<int> 

TYPE1 value = new std::remove_pointer<TYPE1>::type()    // make a new  shared_ptr<int>

关于如何通过 boost 实现这一点有什么想法吗?

最佳答案

variant包含 MPL sequence称为 types,您可以使用它来访问类型。

#include <boost/mpl/at.hpp>

typedef typename boost::mpl::at_c<VType::types, 0>::type TYPE1;

关于c++ - 如何获得 af boost::variant 的第 i 个基础类型(通过 const 整数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469876/

相关文章:

c++ - 避免违反严格别名规则的最简单经验法则?

c++ - HOG vector 包含负值

c++ - 模板显式实例化是否适用于前向声明类型?

c++ - 错误 : expected primary-expression before 'template'

c++ - 模板 typedef c++0x

c++ - 函数调用中参数前的 {} 是什么意思? key_equal{}(key1, key2);

c++ - std::unordered_map 不断导致错误,这是一个错误吗?

c++ - std::next_permutation 实现说明

c++ - 将派生对象传递给模板

c++ - C++ 中复杂的 Typedef