Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-02-15 15:36:55


Jody Hagins wrote:
> That is a very valid option as well. In fact, in his book, Sutter
> mentions that option specifically...
>
> Exceptional C++, page 48:
>
> "If you're one of those folks who like terse code, you can write the
> operator=() canonical form more compactly as:
>
> Stack& operator=(Stack other)
> {
> Swap( other );
> return *this;
> }
> "
>
>
> Not being very up to date on optimization techniques, I do not know
> if a compiler can turn one into better code than the other.

Yes, it can. Consider the case where the definition of operator= is not
visible to the compiler and one does

    s = Stack();

With the by-value version the temporary Stack() will be constructed directly
in 'other', saving a copy.

Of course in a future language with rvalue references the "Stack&& other"
overload will be used instead, with similar or better performance.

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1690.html


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