我对升压侵入式容器很好奇,并想对其进行测试。我基本上从 boost.org 的“如何使用 Boost.Intrusive”一章中复制粘贴了示例。所以我的代码如下所示:
#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());
}
但这不可能是真的,至少我看不到我可能在哪里意外破坏了该对象。我还不知道这些容器的所有细节,所以我会很感激在这里得到一些帮助。