获取std :: variant的基础类型

时间:2018-02-07 18:54:59

标签: c++ variant c++17

快速提问:

是否可以在运行时获取np.int使用的基础类型?

我的第一个猜测就是像这样使用std::variant

decltype()

但我的向量的声明类型是 std::variant<int, float> v; v = 12; std::vector<decltype(v)> vec; 而不是std::vector<std::variant<int, float>>

我知道如何实现这一目标吗? :)

1 个答案:

答案 0 :(得分:5)

  

是否可以在运行时获取std :: variant使用的基础类型?

是的,肯定是

  

我知道如何实现这一目标吗?

尝试实现与“在运行时获取std :: variant使用的基础类型”非常不同的东西,在运行时定义std::vector类型。这是一个非常不同的问题,在这种形式下绝对不可能,因为模板实例化在编译时发生。最近的解决方案是使用std::variant<std::vector<int>, std::vector<float>>并根据变量v

中的运行时信息使用不同的向量