Boost logo

Boost :

Subject: Re: [boost] [smart ptr] Any interest in copy-on-write pointer for C++11?
From: Ralph Tandetzky (ralph.tandetzky_at_[hidden])
Date: 2013-02-08 12:41:28


On 02/08/2013 06:01 PM, Mathias Gaunard wrote:
> On 08/02/13 16:16, Ralph Tandetzky wrote:
>> Hi,
>>
>> is there any interest in a copy-on-write pointer implementation?

> I don't see any reason to make this use lazy copying/COW.
> COW is only useful if you're copying when you do not want to. The
> solution is easy: do not copy if you don't want to; this is made
> easier when you have the option to move instead.
> COW could be used as an alternative implementation for compatibility
> with code that isn't move-aware, but that's essentially the only use
> case.
>
> Now the reason why this isn't more common is that there is no clean
> way to copy an object polymorphically.
>
> There are two approaches:
> - You can use the static type of the object when inserted and assume
> it is the same as the dynamic type. That's not ideal, and this
> requires storing that information separately, adding a bit of space
> overhead. This is the approach boost::any uses.
> - You can make types that need polymorphic copy to have some sort of
> virtual copy constructor, which follows the same principle as other
> virtual functions. A member function of declaration T* clone() const
> is often used, but this has obvious problems with flexibility, since
> it couples allocation and construction. The best you can do is add two
> functions, one that gives the size and alignment, the other that
> constructs an object at a given address. With this approach, even if
> you insert an object from a reference/pointer to base, the derived
> object will correctly get copied, which is a very useful property when
> writing code that uses polymorphism with value objects.
>
> From your description, I take it you're using the first approach.
>
> Finally, and this will be my last complaint with this design, is that
> what you're modeling isn't really a pointer, but rather a smart object.
> It might therefore be worthwhile to change not only the name, but also
> the interface so that construction takes references rather than pointers.
>

Copy-on-write is the most important use case of the class design. Even
larger libraries like Qt use copy-on-write for some types (like QImage
or QPixmap) and make the user interface much easier to use. A major
reason is that value semantics are easier to reason about than reference
semantics. For this purpose cow_ptr<T> is an enabler. Moving isn't
always sufficient.

The class design allows you to copy objects polymorphically. I tried it.
It works. It's useful. I used it in production code.

 From the sematics you're right. It's almost like wrapping an object.
But the syntax is too close to the syntax of other smart pointers, so I
decided to not make it take a reference. The operator-> would not have
an appropriate equivalent.

Ralph Tandetzky


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk