
On Thu, Mar 31, 2011 at 2:07 PM, Jeremy Maitin-Shepard <jeremy@jeremyms.com>wrote:
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.
+1; I believe this had been mentioned before by someone during the XInt review, but I appreciate being reminded of this technique. - Jeff