试图学习boost :: Intrusive Q1

时间:2014-11-07 23:36:39

标签: c++11 boost

我有从互联网上下载的这个程序。如果我在最后添加clear,它会崩溃并显示以下消息:

idf@idf-Satellite-C55t-A ~/Documents/BOOST_INTRUSIVE/Intrusive1/bin/Debug $ ./Intrusive1 
Intrusive1: /usr/local/include/boost/intrusive/detail/utilities.hpp:366: void boost::intrusive::detail::destructor_impl(Hook&, boost::intrusive::detail::link_dispatch<(boost::intrusive::link_mode_type)1u>) [with Hook = boost::intrusive::generic_hook<boost::intrusive::get_list_node_algo<void*>, boost::intrusive::member_tag, (boost::intrusive::link_mode_type)1u, (boost::intrusive::base_hook_type)0u>]: Assertion `!hook.is_linked()' failed.
Aborted
idf@idf-Satellite-C55t-A ~/Documents/BOOST_INTRUSIVE/Intrusive1/bin/Debug $ 

不确定这一切意味着什么。我希望清除所有项目,并删除内容。

#include <vector>
#include <iostream>

#include <boost/intrusive/list.hpp>

using namespace boost::intrusive;

class MyClass : public list_base_hook<>   //This is a derivation hook
{
    int anInt;

public:
    //This is a member hook
    list_member_hook<> member_hook_;

    MyClass(int i)
        :  anInt(i)
    {}
};

//Define a list that will store MyClass using the public base hook
typedef list<MyClass>   BaseList;

//Define a list that will store MyClass using the public member hook
typedef list< MyClass
            , member_hook< MyClass, list_member_hook<>, &MyClass::member_hook_>
            > MemberList;

int main()
{
    typedef std::vector<MyClass>::iterator VectIt;

    //Create several MyClass objects, each one with a different value
    std::vector<MyClass> values;
    for(int i = 0; i < 100; ++i)
        values.push_back(MyClass(i));

    BaseList baselist;
    MemberList memberlist;

    //Now insert them in the reverse order in the base hook list
    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
        baselist.push_front(*it);

    //Now insert them in the same order as in vector in the member hook list
    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
        memberlist.push_back(*it);

    //Now test lists
    {
        BaseList::reverse_iterator rbit(baselist.rbegin());
        MemberList::iterator mit(memberlist.begin());
        VectIt  it(values.begin()), itend(values.end());

        //Test the objects inserted in the base hook list
        for(; it != itend; ++it, ++rbit)
            if(&*rbit != &*it)   return 1;

        //Test the objects inserted in the member hook list
        for(it = values.begin(); it != itend; ++it, ++mit)
            if(&*mit != &*it)    return 1;

        values.clear();
        //Now delete all the values. Do they dissapear from all containers?
           //Now insert them in the reverse order in the base hook list
        //for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
        //    ;

        //std::cout << values.size();
        //std::cout << baselist.size();
        //std::cout << memberlist.size();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

侵入式容器不会拥有元素。元素必须存储在带外。

您正在做的是删除元素,而它们仍然在逻辑上插入(几个)侵入式集合(即它们通过其侵入式挂钩链接 < / em>的)。

safe mode中,Boost Intrusive实际上会在钩子结构的析构函数中诊断出这个,​​这就是你得到错误信息的原因。

另请参阅:auto-unlink hooks