views:

177

answers:

2

Does there exist a collection, that is aware of shared_ptr internals, and avoids regular copying of stored shared_ptr elements in favor of just copying their internal weak pointer?

This implicitly means, that no constructor/destructor calls will be done and that there will be no manipulation of shared_ptrs' reference counters.

+2  A: 

that is aware of shared_ptr internals,

That should answer your question right there. To be aware of the internals, such a collection would almost certainly have to be part of boost's smart pointer libraries. Unfortunately, there is no such thing.

This is indeed a downside to smart pointers. I would recommend using data structures that limit the number of copies done internally. Vector's reallocations will be painful. Perhaps a deque, which has a chunked based allocation, would be useful. Keep in mind too that vector implementations tend to get new memory in exponentially increasing chunks. So they don't reallocate, say, every 10 elements. Instead you might start out with 128 elements, then the vector reserves itself 256, then moves up to 512, 1024, etc. Each time doubling what is needed.

Short of this there's boost's ptr_vector or preallocating your data structures with enough space to prevent internal copying.

Doug T.
+7  A: 

In theory, after C++0x is adopted, containers will be modified to use move semantics where appropriate. At that point, shared_ptr can also be modified to have a move constructor to minimize unnecessary reference count adjustment.

MSN
In the C++0x draft, shared_ptr already has this behaviour.
James Hopkin
Well then there you go! :)
MSN