I came upon https://web.archive.org/web/20120707045924/cpp-next.com/archive/2009/08/want-speed-pass-by-value/
Author's Advice:
Don’t copy your function arguments. Instead, pass them by value and let the compiler do the copying.
However, I don't quite get what benefits are gained in the two example presented in the article:
// Don't
T& T::operator=(T const& x) // x is a reference to the source
{
T tmp(x); // copy construction of tmp does the hard work
swap(*this, tmp); // trade our resources for tmp's
return *this; // our (old) resources get destroyed with tmp
}
vs
// DO
T& operator=(T x) // x is a copy of the source; hard work already done
{
swap(*this, x); // trade our resources for x's
return *this; // our (old) resources get destroyed with x
}
In both cases one extra variable is created, so where are the benefits? The only benefit I see, is if the temp object is passed into second example.
obj = T();
orobj = foo();
wherefoo()
returns aT
. – Mopey