【发布时间】:2024-04-29 14:15:03
【问题描述】:
我有以下代码 (play with example)。它检查variant 的底层价值是什么,并使用get 接收这个底层价值。如您所见,代码变得非常重复。
#include <variant>
#include <string>
#include <iostream>
#include <vector>
int main()
{
std::variant<int, double, std::string> v;
// v holds a string
v = "hi there!";
switch(v.index()) {
case 0:
{ // brackets to avoid cross initialization
int i = std::get<int>(v);
// handle int
}
break;
case 1:
{
double d = std::get<double>(v);
// handle double
}
break;
case 2:
{
std::string s = std::get<std::string>(v);
// handle string
}
break;
}
}
问题:有没有办法将变体的索引作为constexpr 变量获取?我想做这样的事情:
// what I would like to do
// auto val_of_v = std::get<v.index()>(v);
// Error: template argument deduction/substitution failed
// in 'constexpr' expansion of v<...>::index()
【问题讨论】:
-
如果变量不是 constexpr,那么它所持有的类型在编译时是无法知道的。
标签: c++ switch-statement c++17 constexpr variant