Boost.intrusive multiple containers

The boost.intrusive document mentions the use of multiple containers for storage in a single object. However, there is no example, so I made my own. Is this right 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 (for example, adding another DefaultList), it will result in an error. Is this intended? Why are we not allowed to use a second container of the same type?

+4
source share
1 answer

You're close Here's how it should look:

 #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 used when you want to inherit a hook. list_member_hook is the time you want the hook to be a member. In addition, push_back accepts a link, not a pointer.

Here is an example of using hook elements:

 #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); } 
+3
source

All Articles