[英]C++: assignment operator: pass-by-value (copy-and-swap) vs pass-by-reference
Considering the advantages of copy-and-swap idiom ...考虑到复制和交换习语的优点......
Why do we still need copy-assignment operator accepting references as the mainstream?为什么我们仍然需要接受引用作为主流的复制赋值运算符?
class T {
public:
// 1: Why still commonly this?
T& operator=(const T& rhs);
// 2: Why not mostly that?
T& operator=(T rhs);
}
There are answers suggesting to use the latter ( here and here ).有答案建议使用后者(此处和此处)。
However, most of the SO examples are still around pass-by-reference operator=
.但是,大多数 SO 示例仍然围绕 pass-by-reference
operator=
。
Even consolidated C++ FAQ points out (yes, it's about const
, but...):甚至合并 C++ 常见问题解答指出(是的,它是关于
const
,但是......):
A
class Fred
's copy constructor and assignment operator should haveconst
in the parameter: respectivelyFred::Fred(const Fred&)
andFred& Fred::operator=(const Fred&)
A
class Fred
的复制构造函数和赋值运算符应在参数中具有const
:分别为Fred::Fred(const Fred&)
和Fred& Fred::operator=(const Fred&)
Obviously, copy-and-swap is implementable via pass-by-reference - it is just unnecessary if copy is to be made anyway.显然,复制和交换是可以通过引用传递来实现的——如果无论如何都要进行复制,那就没有必要了。 One may also want to avoid copying immediately on call (perform it conditionally in the body) - isn't that the less frequent case (possibly premature optimization) then?
人们可能还想避免在调用时立即复制(在正文中有条件地执行它)——那不是不太常见的情况(可能是过早的优化)吗?
Shouldn't copy-and-swap with pass-by-value assignment be the default approach?不应该使用按值传递的复制和交换作为默认方法吗?
Pass by reference is for avoiding unnecessary copy when you need faster executions, and pass by a const reference is when you want to pass it fast and read-only.通过引用传递是为了在您需要更快的执行时避免不必要的复制,而通过 const 引用传递是当您想要快速和只读地传递它时。 And pass by copy is when you want to copy the object to be able to manipulate it in the course of the execution/implementation of your function.
并且通过副本是当您想要复制 object 以便能够在 function 的执行/实施过程中对其进行操作。
More than it creates spurious non useful copies, second option may not work on every class as copying may be deleted.除了创建虚假的无用副本之外,第二个选项可能不适用于每个 class,因为复制可能会被删除。
First option have many, if not all, advantages: no copy, read-only semantic, always available.第一个选项有很多(如果不是全部)优点:无副本、只读语义、始终可用。
I was pointed to the valuable hint :我被指出了有价值的提示:
For example, ever wonder why high performance / often used std::types like
std::vector
andstd::string
don't use copy/swap?例如,有没有想过为什么高性能/经常使用的 std::types 像
std::vector
和std::string
不使用复制/交换? || Howard Hinnant
霍华德·欣南特
Everything I found against copy-on-swap was about optimization (avoid copy, re-purpose lhs
):我发现的所有反对 copy-on-swap 的都是关于优化(避免复制,重新利用
lhs
):
Another common possibility happens in (STL) container when lhs
has space for rhs
:当
lhs
有rhs
空间时,另一种常见的可能性发生在 (STL)容器中:
class container<T> { public: // If passed by value, created copy would unconditionally increase capacity: T& operator=(const container<T>& rhs) { //... if (this->capacity() >= rhs.size()) { // reuse capacity... } else { // increase capacity... } } }
Notice that the hint also mentions (STL) containers .请注意,提示还提到了 (STL)容器。
The trade-off for operator=
is: operator=
的权衡是:
Related answers:相关答案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.