Boost logo

Boost :

Subject: Re: [boost] copy on write for std containers
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-12-05 06:11:09

On 05/12/2010 01:11, Peter Foelsche wrote:

> Anybody know in which cases the compiler optimizes away
> or does not optimize away the call to the copy-constructor
> when returning an object by value?

Whenever the object returned is a temporary or local variable, and the
result of the function is used to initialize a new object.

You can also benefit from this optimization in assignments by writing
your assignment operator as

type& operator(type other)
     return *this;

since the return of the function is then used to directly initialize
'other' without any copy.

> I still think that something like this should be available in some
> library for people,
> which cannot write such a wrapper in a few minutes by themselves.

To make such a solution generic, you would have to be able to make a
good "smart reference" first, which isn't possible due to lack of
operator. overloading.

Boost list run by bdawes at, gregod at, cpdaniel at, john at