Date: 2002-02-05 16:41:16
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> first, second;
// Do various operations on 'first' and 'second' here.
result.swap( (first.size() > second.size()) ? first : second );
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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk