所以我有:
typedef std::variant<int,float,std::string> VarType;我希望能够做到:
VarType a = 1;
VarType b = 1;
VarType c = a + b;当类型混合时,它抛出是很酷的。
发布于 2018-06-26 17:45:46
VarType c = std::get<int>(a) + std::get<int>(b);更一般的情况:
VarType c = std::visit([](auto x, auto y) -> VarType
{
if constexpr(!std::is_same_v<decltype(x), decltype(y)>)
{
throw;
}
else
{
return x + y;
}
}, a, b);https://stackoverflow.com/questions/51039758
复制相似问题