c++ - boost 变体 : get reference to the variant by its type

标签 c++ boost variant

假设有

boost::variant<int, std::string> v;

如何获得类型为 std::string & 的引用 x 可以像这样使用

x = "abc";

并且会有等同于

的行为
v = "abc";

?

最佳答案

std::string& x = boost::get<std::string>(v);

关于c++ - boost 变体 : get reference to the variant by its type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33227003/

相关文章:

c++ - 拥有 std::map 的最佳方法,如果没有 key ,我可以在其中定义返回的内容?

python - 使用 boost-python 将 python 变量设置为 C++ 对象指针

c++ - 如何在具有模板函数的类中使用类型安全的 union (变体)?

arrays - 变体数组可以有 0 个元素吗?

c++ - 初始化构造函数 C++

C++ 头文件顺序

c++ - boost 序列化保存到多种存档类型并防止在使用指针加载时构建新对象

c++ - boost ptr_map 替换值

c++ - boost库应该依赖于结构成员对齐吗?

c++ - std::变体反射。我如何知道分配了哪种类型的值 std::variant?