我遇到了 https://web.archive.org/web/20120707045924/cpp-next.com/archive/2009/08/want-speed-pass-by-value/
作者的建议:
不要复制你的函数参数。相反,按值传递它们并让编译器进行复制。
但是,我不太明白在文章中介绍的两个示例中获得了哪些好处:
// 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
}
对比
// 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
}
在这两种情况下,都会创建一个额外的变量,那么好处在哪里?我看到的唯一好处是,如果将临时对象传递到第二个示例中。