Reputation: 18552
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?
Upvotes: 4
Views: 1573
Reputation: 29981
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);
}
Upvotes: 3