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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#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;
agList 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?
I am a bit out of my depth, but I'll give it a go as you haven't had a response yet. You may be better on the boost mailing list though.
When you inherit from one of the hooks, you are getting the appropriate data members to store the container information. So for a doubly linked list you would get a nextnode* and a prevnode*. You only get one of each. So if you try and put your object in two containers of the same type, where would the second store its nextnode and prevnode? You can't inherit from the same base class multiple times so you can't have two copies of these variables. Even if you could, how would the container know which to use?
Personally, I really don't see the need for this in boost though. I think it is something that is needed quite rarely, and every time it has to be done, speed is critical, so it may be better to write a customized solution.
I can't remember the project, but I wrote a multi indexed container for something many years ago. I remember I needed to keep a lot of data sorted by different criteria. That would have perhaps benefitted from something like this. But the solution of using a std::vector<Object> and a std::vector<int> as in index into the main array for each criteria was pretty trivial and adequately fast.