使用 std::get 访问 std::variant<index>
Accessing std::variant using std::get<index>
如何使用v.index()
然后使用std::get<index>(v)
访问变体的成员?
当变体具有多个相同类型的条目时非常有用。
以下内容不起作用。此代码既不能在GCC上编译,也不能在clang 上编译
#include <iostream>
#include <variant>
#include <string>
#include <sstream>
typedef std::variant<int, int, std::string> foo;
std::string bar(const foo f) {
const std::size_t fi = f.index();
auto ff = std::get<fi>(f);
std::ostringstream ss;
ss << "Index:" << fi << " Value: " << ff;
return ss.str();
}
int main()
{
foo f( 0 );
std::cout << bar(f);
}
std::get当然有很多版本,所以错误消息很长。
gcc抱怨(对于每个版本的get<>(
prog.cc:10:29: error: the value of 'fi' is not usable in a constant expression
auto ff = std::get<fi>(f);
^
prog.cc:9:23: note: 'fi' was not initialized with a constant expression
const std::size_t fi = f.index();
^~
prog.cc:10:29: note: in template argument for type 'long unsigned int'
auto ff = std::get<fi>(f);
Clang抱怨(对于每个版本的get<>((re_Tp或_Ip,视情况而定(
candidate template ignored: invalid explicitly-specified argument for template parameter '_Tp'
Wandbox
更新后询问如何解决,而不是错误消息的含义。
std::get<>
适用于请求在编译时已知的变量索引。
如果您需要对一个直到运行时才知道其类型的变量值执行操作,惯用方法是使用具有std::visit
的访问者。
#include <iostream>
#include <variant>
#include <string>
struct output_visitor
{
template< typename T >
void operator() ( const T& value ) const
{
std::cout << value;
}
};
int main()
{
std::variant<int, std::string> f( 0 );
std::visit( output_visitor{}, f );
}
这通常可以用C++14"来实现;通用lambdas";
#include <iostream>
#include <variant>
#include <string>
int main()
{
std::variant<int, std::string> f( 0 );
std::visit( [](auto v){std::cout << v;} , f );
}
gcc 8.1的错误输出还包括解释:
<source>:10:29: error: the value of 'fi' is not usable in a constant expression auto ff = std::get<fi>(f); ^ <source>:9:23: note: 'fi' was not initialized with a constant expression const std::size_t fi = f.index();
整数模板参数必须是常量表达式。f
不是一个常量表达式,因此对其非静态成员函数的调用不是一个常数表达式,因此fi
不是。
您可以使用获得更好的错误消息
constexpr std::size_t fi = f.index();
只有当f
也被声明为constexpr
时,代码get<fi>(f)
才能工作;但只有当变体中的所有类型都有平凡的析构函数时,这才有可能,而std::string
没有。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>
- 如何获取std::vector<DMatch>