boost::variant 出现问题,链接器出现段错误
我有一个有 7 种类型的 boost 变体。当我尝试使用最后两种类型时,链接器出现段错误。我在 64 位 Linux 机器上使用 g++(SuSE Linux 上的 gcc 版本 3.3.3),得到的错误是
collect2: ld terminated with signal 11 [Segmentation fault]
无论我将类型放入什么顺序,最后两个都会在我尝试使用时导致段错误他们。有什么想法为什么会发生这种情况吗?
代码:
typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;
typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
Class4Tuple, Class5Tuple, Class6Tuple,
Class7Tuple > ClassTupleItem;
ClassX::Ptr 是指向该类的 boost 共享指针。 Ptr 被定义为类本身内部的 typedef ,
struct Class1
{
typedef boost::shared_ptr<Class1> Ptr;
...
...
}
当我尝试使用 boost 变体中的最后两种类型时,
Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple
Class6Tuple tup2("str", pointer2);
ClassTupleItem(tup2); // causes a segfault.
就像我将 boost::variant 定义为(交换 Class6 和 Class1)一样,
typedef boost::variant< Class6Tuple, Class2Tuple, Class3Tuple,
Class4Tuple, Class5Tuple, Class1Tuple,
Class7Tuple > ClassTupleItem;
然后在编译时出现段错误代码
Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // worked earlier
I have a boost variant with 7 types in it. When I try to use the last two types, the linker segfaults. I am using g++ (gcc version 3.3.3 on SuSE Linux) on a 64 bit linux machine and the error that I get is
collect2: ld terminated with signal 11 [Segmentation fault]
It doesnt matter what order I put the types in, the last two will cause a segfault when I try to use them. Any ideas why this would be happening?
Code:
typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;
typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
Class4Tuple, Class5Tuple, Class6Tuple,
Class7Tuple > ClassTupleItem;
ClassX::Ptr is a boost shared pointer to that class. Ptr is defined as a typedef inside the class itself as below
struct Class1
{
typedef boost::shared_ptr<Class1> Ptr;
...
...
}
when I try to use the last two types in the boost variant as in
Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple
Class6Tuple tup2("str", pointer2);
ClassTupleItem(tup2); // causes a segfault.
if I define the boost::variant as (interchange Class6 and Class1)
typedef boost::variant< Class6Tuple, Class2Tuple, Class3Tuple,
Class4Tuple, Class5Tuple, Class1Tuple,
Class7Tuple > ClassTupleItem;
then I get a segfault when compiling this code
Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // worked earlier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它看起来像是一个编译器/链接器错误:任何 C++ 代码都不应该在编译器/链接器中导致段错误。
顺便问一下,如何编译这段代码?
指针
是如何声明的?如果类是这样声明的,对于
Class1Tuple
,pointer
应该是shared_ptr
,对于Class6Tuple
> 它应该是不同的类型,shared_ptr
。编辑:以下代码可以使用 g++ 3.3.6 正确编译。目前我无法在 gcc 3.3.3 和 SUSE Linux 上测试它。请尝试编译它并查看链接器是否仍然出现段错误。
It looks like a compiler/linker bug: no C++ code should ever cause segfaults in the compiler/linker.
By the way, how do you get this code to compile? How is
pointer
declared?If the classes are declared like this, for
Class1Tuple
,pointer
should be ashared_ptr<Class1>
, and forClass6Tuple
it should be of a different type,shared_ptr<Class6>
.Edit: The following code compiles correctly with g++ 3.3.6. I am not able to test it on gcc 3.3.3 and SUSE Linux at the moment. Please try to compile this and see if the linker still gives a segfault.