简体   繁体   English

C++ Boost 侵入式列表 - 示例

[英]C++ Boost Intrusive List - Example

I am curious about the boost intrusive containers and wanted to test it.我对 boost 侵入式容器很好奇并想测试它。 I basically copy pasted the example from boost.org in the chapter "How to use Boost.Intrusive".我基本上是从 boost.org 的“如何使用 Boost.Intrusive”一章中复制粘贴的示例。 So my Code looks like this:所以我的代码如下所示:

#include <iostream>
#include <boost/intrusive/list.hpp>

using namespace boost::intrusive;

struct test_tag1;
struct test_tag2;

typedef list_base_hook< tag<test_tag1> > BaseHook;
typedef list_base_hook< tag<test_tag2> > BaseHook2;

class TestClass : public BaseHook, public BaseHook2 {
    public:
        int test_var;
};

typedef list< TestClass, base_hook<BaseHook> > class_list;
typedef list< TestClass, base_hook<BaseHook2> > class_list2;

int main() {
    class_list list;

    TestClass class1 = TestClass();
    list.push_back(class1);

    bool is_the_same = (&list.front() == &class1);
    std::cout << is_the_same;

    return 0;    
}

It compiles successfully, but on execution I keep getting the following error:它编译成功,但在执行时我不断收到以下错误:

1Assertion failed: !hook.is_linked(), file boost/intrusive/detail/generic_hook.hpp, line 47

I opened the generic_hook.hpp to check what raises this error, and the description of the assert is:我打开了 generic_hook.hpp 来检查是什么引发了这个错误,断言的描述是:

void destructor_impl(Hook &hook, detail::link_dispatch<safe_link>)
{  //If this assertion raises, you might have destroyed an object
   //while it was still inserted in a container that is alive.
   //If so, remove the object from the container before destroying it.
   (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked());
}

But that cant be true, at least I cant see where I could have destroyed the object accidently.但这不可能是真的,至少我看不出哪里可能会不小心破坏了对象。 I dont know all the details about these containers yet, so I would be gratefull to get some help here.我还不知道关于这些容器的所有细节,所以我很感激能在这里得到一些帮助。

You declared class1 after list .list之后声明了class1 So when main exits, class1 is destroyed before list is.所以当main退出时, class1list之前被销毁。 (Destruction in reverse order of construction.) (以与构造相反的顺序销毁。)

You have inserted class1 into list .您已将class1插入list So when list is destroyed, class1 , which is still inserted into the container, is not alive anymore.所以当list被销毁时,仍然插入容器中的class1不再存在。

Move the declaration of class1 before that of list , so that it will be destroyed later.class1的声明class1 list之前,以便稍后销毁。

Compare also to the boost documentation from which you probably constructed the code in your question.还要与您可能从中构建问题中的代码的 boost 文档进行比较。 There the element object is also declared before the list.元素对象也在列表之前声明。

In general the note at the bottom of the linked documentation page is important.通常,链接文档页面底部的注释很重要。 It says that the container only stores a reference and that you must make sure that the inserted elements stay alive longer than the container.它说容器只存储一个引用,并且你必须确保插入的元素比容器存活的时间更长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM