Boost logo

Boost :

From: brangdon_at_[hidden]
Date: 2002-02-05 16:41:16

In-Reply-To: <000f01c1adeb$5e0091e0$050110ac_at_[hidden]>
On Mon, 4 Feb 2002 19:18:14 -0700 Rainer Deyke (root_at_[hidden])
> In theory the compiler is allowed to do anything at all so long as
> outward semantics are preserved, but in practice there are cases where
> a move-constructor is necessary for performance. 'f' right now is
> pretty trivial, but consider a less trivial example:
> std::vector<int> g()
> {
> std::vector<int> first, second;
> // Do various operations on 'first' and 'second' here.
> return (first.size() > second.size()) ? first : second;
> }

I think this can be rewritten as:

 std::vector<int> g()
   std::vector<int> first, second;
   // Do various operations on 'first' and 'second' here.
   std::vector<int> result;
   result.swap( (first.size() > second.size()) ? first : second );
   return result;

If vector is written not to allocate in its default constructor (and
usually it is), and the compiler uses the named return value optimisation,
this will not do any unnecessary allocations or copies.

Of course it would be nicer not to have to do this.

-- Dave Harris

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