开发者

Boost.intrusive multiple containers

In the boost.intrusive document, it mentions about using multiple containers to store in one object. However, there's no actual example, so I made my own. Is this the right way to do?

#include <boost/intrusive/list.hpp>
struct tag1;
class A:public list_member_hook<>, public list_member_hook<tag<tag1> >
{
}

typedef list_base_hook<tag<tag1> > TagHook;
typedef list<A> DefaultList;
typedef list<A, base_hook<TagHook> > TagList;

int main()
{
     DefaultList dList;
     TagList tList;
     A开发者_高级运维 *a = new A();
     dList.push_back(a);
     tList.push_back(a);
}

If I add another container of the same type (such as adding another DefaultList), it will produce error. Is this intended? Why are we not allowed to use the second container of the same type?


You're close. Here's what it should look like:

#include <boost/intrusive/list.hpp>
struct tag1;
class A:public list_base_hook<>, public list_base_hook<tag<tag1> >
{
}

typedef list_base_hook<tag<tag1> > TagHook;
typedef list<A> DefaultList;
typedef list<A, base_hook<TagHook> > TagList;

int main()
{
     DefaultList dList;
     TagList tList;
     A *a = new A();
     dList.push_back(*a);
     tList.push_back(*a);
}

list_base_hook is used when you want to inherit the hook. list_member_hook is for when you want the hook to be a member. Also, push_back takes a reference, not a pointer.

Here's an example using member hooks:

#include <boost/intrusive/list.hpp>
class A
{
public:
    list_member_hook<> m_hook1, m_hook2;
}

typedef list<A, member_hook<A, list_member_hook<>, &A::m_hook1> > List1;
typedef list<A, member_hook<A, list_member_hook<>, &A::m_hook2> > List2;

int main()
{
     List1 list1;
     List2 list2;
     A *a = new A();
     list1.push_back(*a);
     list2.push_back(*a);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜