|
Boost : |
From: Andrei Alexandrescu (andrewalex_at_[hidden])
Date: 2002-10-12 11:56:32
"Alexander Terekhov" <terekhov_at_[hidden]> wrote in message
news:3DA80A28.93230672_at_web.de...
> T& operator=(T temp) throw()
> { return this->swap(temp); }
>
> What's wrong with it (other than "way too strong" argument, please)?
This is the correct signature and implementation (except that I believe
'swap' should return void, returning a reference is too confusing, but let's
not dwell on that, it's a minor point).
The pass-by-value is important because it communicates to the compiler what
the programmer is doing. The programmer lets the compiler take care of
creating the temporary instead of creating it by herself.
I recently reached the conclusion that taking a parameter by const reference
just to make a copy of it inside the function is a "lie". The signature
says: "I don't need a value! A reference to a const is all I need!" and the
code says: "The first line of this function makes a copy!" There will be a
section in my upcoming article entitled "The Lying const". Taking const& T
as arguments in /any/ function when you actually *do* need a copy chokes the
compiler (and Zuto) and practically forbids them to make important
optimizations.
Andrei
-- All new! THE C++ Seminar: Oct. 28-30 in Vancouver, WA. http://www.thecppseminar.com/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk