考虑:
#include <variant>
#include <iostream>
int main()
{
double foo = 666.666;
std::variant<double, uint64_t> v = foo;
std::cout << std::get<uint64_t>(v) << std::endl;
return 0;
}
这导致:
terminate called after throwing an instance of 'std::bad_variant_access'
what(): Unexpected index
Aborted (core dumped)
为什么?
我明白这std::variant
将是一个替代品union
。但是,如果这种基本的东西失败了,它有什么用呢?