
I thought about it too, that's very easy to do. Something like: cow_wrapper<std::vector<Big> > a( new std::vector<Big>() ); a->push_back( Big() ); // compilation error, as operator-> returns non-const. a.get_unique().push_back( Big() ); // OK. Just yet another smart pointer. May be added to Boost.SmartPtr. 2011/4/1 Jeremy Maitin-Shepard <jeremy@jeremyms.com>
On 03/31/2011 01:48 PM, Alexandr Sterligov wrote:
Yes, sorry... I will be more careful with calling anything STL-compliant. It is only STL-like.
It seems that using a generic COW wrapper might be more suitable. Internally it would maintain a pointer to a reference-counted instance of the base type. It would provide a method for getting a const reference to the type (e.g. get(), operator*() and operator->()), and also a method for obtaining a non-const reference to a unique copy of the type (e.g. get_unique()), which would take care of copying if the reference count is greater than 1.
I believe this interface is better than a specialized type that tries to mimic the interface of a particular container because generally if there is a need to mutate the type, it will be done by a series of possibly many mutating operations on the type/iterations, and it is inefficient to check if there is a need to copy e.g. on each iterator dereference or on each invocation of operator[].
It also avoids the need to create the specialized types in the first place; a single quite simple wrapper type will solve the problem for all types.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Best regards, Alexander Sterligov