C++ Boost 侵入式列表 - 示例
C++ Boost Intrusive List - Example
我对 boost 侵入式容器很好奇,想测试一下。我基本上是在 "How to use Boost.Intrusive" 一章中复制粘贴了 boost.org 中的示例。所以我的代码看起来像这样:
#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;
}
它编译成功,但在执行时我不断收到以下错误:
1Assertion failed: !hook.is_linked(), file boost/intrusive/detail/generic_hook.hpp, line 47
我打开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());
}
但这不可能是真的,至少我看不出我可能在哪里不小心破坏了这个物体。
我还不知道关于这些容器的所有细节,所以我很感激能在这里得到一些帮助。
您在 list
之后声明了 class1
。所以当 main
退出时, class1
在 之前被销毁 list
是。 (破坏顺序与构造相反。)
您已将 class1
插入 list
。所以当 list
被销毁时,仍然插入容器中的 class1
已经不存在了。
将class1
的声明移到list
的声明之前,以便稍后销毁。
还可以与 the boost documentation 进行比较,您可能从中构建了问题中的代码。元素对象也在列表之前声明。
一般来说,链接文档页面底部的注释很重要。它说容器只存储一个引用,你必须确保插入的元素比容器存活的时间更长。
我对 boost 侵入式容器很好奇,想测试一下。我基本上是在 "How to use Boost.Intrusive" 一章中复制粘贴了 boost.org 中的示例。所以我的代码看起来像这样:
#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;
}
它编译成功,但在执行时我不断收到以下错误:
1Assertion failed: !hook.is_linked(), file boost/intrusive/detail/generic_hook.hpp, line 47
我打开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());
}
但这不可能是真的,至少我看不出我可能在哪里不小心破坏了这个物体。 我还不知道关于这些容器的所有细节,所以我很感激能在这里得到一些帮助。
您在 list
之后声明了 class1
。所以当 main
退出时, class1
在 之前被销毁 list
是。 (破坏顺序与构造相反。)
您已将 class1
插入 list
。所以当 list
被销毁时,仍然插入容器中的 class1
已经不存在了。
将class1
的声明移到list
的声明之前,以便稍后销毁。
还可以与 the boost documentation 进行比较,您可能从中构建了问题中的代码。元素对象也在列表之前声明。
一般来说,链接文档页面底部的注释很重要。它说容器只存储一个引用,你必须确保插入的元素比容器存活的时间更长。