从 boost::variant 生成分段错误中获取 int
Get int from boost::variant generate segmentation fault
我正在尝试从boost::variant中获取int值。代码生成分段错误 - 为什么?我将注释放在代码中,这些行会产生错误。我以为
int numberInt = boost::get<int>(v);
将无法正常工作,所以我将其更改为
int *ptrInt = boost::get<int>(&v);
哪个正在编译,但我仍然无法获得 int 值?与双倍完全相同。字符串类型正在工作。
#include <iostream>
#include "boost/variant.hpp"
#include <boost/variant/get.hpp>
using namespace std;
int main(int argc, char* argv[])
{
boost::variant<int, double, std::string> v;
v = 16;
v = 3.1415;
v = "hello new year";
//int numberInt = boost::get<int>(v); //1) not working
//double numberDouble = boost::get<double>(v);//2) not working
int *ptrInt = boost::get<int>(&v); //3) compiling
if(ptrInt)
cout << *ptrInt << endl; //4) not displayed
//cout << *ptrInt << endl; //5) segmentation fault
double *ptrDouble = boost::get<double>(&v); //6) compiling
if(ptrDouble)
cout << *ptrDouble << endl; //7) not displayed
//cout << *ptrDouble << endl; //8) segmentation fault
std::string caption = boost::get<string>(v);
cout << caption << endl; //9) working
return 0;
}
// clear && clear && g++ test.cpp -std=c++11 -o test && ./test
我想你误解了什么是提升变体。库的文档将variant
类型描述为"多类型,单个值"。(强调我的)。由于您已经分配了类型 std::string
的值,因此不会在variant
中存储其他类型的值。variant
的一个好处(与union
相比)在get
函数的注释中有所描述:
// Retrieves content of given variant object if content is of type T.
// Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
因此,如果int numberInt = boost::get<int>(v);
正常工作,它应该引发异常。int *ptrInt = boost::get<int>(&v);
应该返回一个空指针。取消引用空指针是未定义的行为,可能是分段错误的原因。
我认为您正在寻找的行为是tuple
(在 boost 和 std 中都有)。如果您不介意为成员对象命名,一个简单的结构/类也可以。
怕你不明白boost::variant
是如何工作的。在类型论中,boost::variant
是和类型,或Algebraic Data Type
。
这通常也被称为"歧视工会",基本上看起来像(在这种情况下):
struct Variant {
size_t index;
union {
int a;
double b;
std::string c;
} u;
};
现在,当你写v = 16
时,会发生什么是:
v.u.a = 16; v.index = 0;
当你写v = 3.1415
时,会发生什么是:
v.u.b = 3.1415; v.index = 1;
最后,当你写v = "hello new year"
时,发生的事情是:
v.u.c = "hello new year"; v.index = 2;
请注意,每次都会更新表示当前处于活动状态的union
成员的index
...因此,在任何时间点,只有一个工会成员处于活跃状态。
当您使用boost::get<int>(&v)
代码实际上如下所示:
int* get_0(Variant* v) {
if (v && v->index == 0) { return &v->u.a; }
return nullptr;
}
因此,由于此时v->index
是2
的,因此它返回一个nullptr
。
唯一有效的get
是boost::get<std::string>(&v)
,因为它检查index
是否2
,因此返回指向v.u.c
的指针。
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 使用 strcat 获取缓冲区溢出错误
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 在 alglib::real_1d_array 中获取元素总和的奇怪错误
- 无法使函数公开。获取:"LNK2005"错误。如何调试链接器错误
- 为什么我在空指针错误(链表)中获取成员访问权限
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 获取毫秒延迟的错误值
- 获取 clang++:错误:在编译文件系统库的代码时
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 如何在常量计算表达式中获取编译时错误?
- 获取"rw_ssid"中成员"长度"的错误请求,该成员属于非类类型"char*"
- Assimp 库错误:获取打包成员的地址
- 错误:获取临时地址
- [错误]获取C++中临时数组的地址
- 错误:获取临时[-fpermissive] -npc_multivendor的地址
- C++:为“char [4]”数组错误获取太多初始值设定项
- 字符串数据成员的指针错误 - 获取访问冲突
- 调用派生类时 - 错误:获取临时 [-fallowive] 的地址