[ptr_map] Boost pointer containers, where is the working map type?

http://www.boost.org/libs/ptr_container/doc/reference.html I'm trying to use the boost::ptr_map class, and either I'm misunderstanding it's usage or syntax. I originally assumed that a ptr_map class is a map container of pointers and/or container of abstract objects via pointers. However, the example code in the following link, does not seem to be using pointers at all. http://www.boost.org/libs/ptr_container/doc/reference.html Moreover, if I attempt to use an abstract pointer in either the first or second type, I get compile errors. I also tried creating a ptr_map to a base class with a normal virtual function, but when I ran my test, I can see that it slices the derived class. So exactly what is the ptr_map class for, and does anyone have example code that will work with an abstract pointer? I'm trying to compare using the boost pointer containers to using a clone smart pointer. Which boost container would be able to give me functionallity like the following: map<cow_ptr<Shape>, int> mShapeToInt; map<int, cow_ptr<Shape> > mIntToShape; Where Shape is an abstract base class, and cow_ptr is the following: http://code.axter.com/cow_ptr.h

"Slawomir Lisznianski" <public@paramay.com> wrote in message news:<donook$li2$1@sea.gmane.org>...
Hello,
I've been using ptr_map lately and have a few questions regarding its interface.
Below is a signature of `insert' function as declared in
axter wrote: ptr_map_adapter:
std::pair<iterator,bool> insert( key_type& k, value_type x );
Why is `k' a non-const reference?
it's a matter about exception-safety. see the FAQ.
To imply ownership transfer, couldn't `x' be of `std::auto_ptr<value_type>' type instead?
right. the next release will have overloads taking auto_ptr<T> as well as T*.
What happend to std::pair as an argument of insert? Was symmetry with std::map dropped for a reason here?
yes, exception-safety.
From my test, it doesn't seem as though ptr_map has consistent and reliable
implementation.
Do you care to explain in detail what that means?
I recommend that boost replace the pointer containers with the following smart pointers: http://code.axter.com/copy_ptr.h http://code.axter.com/cow_ptr.h
Example usage: std::map<int, cow_ptr<foo> > MyPtrMap;
The above type gives you more functionality then the boost::ptr_map type, and of course, it's interface matches the std::map interface. Where as the boost::ptr_map type has the following problems: 1. Doesn't allow you to use an abstract pointer
Really?
2. Requires a non-constant argument for the insert function
Right, to give exception-safety.
3. Requires value semantics for the operator[] function
?
4. When assigning value via operator[] from one container to another, you get object splicing for derived types
So?
5. It doesn't seem to compile on VC++ 6.0, and when it compiles on VC++ 7.1 you get compiler warnings
Yes, vc7.1 warns about a lot of things, particular ADL. Turn it off.
If you use cow_ptr instead, you don't have the above problems.
There are similar problems in the boost::ptr_set class, which also can be replaced with cow_ptr or copy_ptr std::set<cow_ptr<foo> > MyPtrSet;
IMHO, the entire boost pointer container set of classes can be replace with either cow_ptr and/or copy_ptr, and the result would be a more reliable,
reliable in what way? Switching to a deep-copying smart pointer can have significant performance implications, particularly (but not only) with compilers without RTO, like vc6. -Thorsten
participants (2)
-
axter
-
Thorsten Ottosen