简体   繁体   English

C++:赋值运算符:按值传递(复制和交换)与按引用传递

[英]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 have const in the parameter: respectively Fred::Fred(const Fred&) and Fred& 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 and std::string don't use copy/swap?例如,有没有想过为什么高性能/经常使用的 std::types 像std::vectorstd::string不使用复制/交换? | | Howard Hinnant霍华德·欣南特

Everything I found against copy-on-swap was about optimization (avoid copy, re-purpose lhs ):我发现的所有反对 copy-on-swap 的都是关于优化(避免复制,重新利用lhs ):

  • Obviously, self-assignment is noop.显然,自分配是无用的。
  • Another common possibility happens in (STL) container when lhs has space for rhs :lhsrhs空间时,另一种常见的可能性发生在 (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=的权衡是:

  • by value + copy-and-swap: simplifies (saves human time) with strong exception guarantees按价值 + 复制和交换:通过强大的异常保证简化(节省人力时间)
  • by reference: allows optimization (saves machine time) likely weakening guarantees通过参考:允许优化(节省机器时间)可能削弱保证

Related answers:相关答案:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++中按值传递和按引用传递之间的区别 - Differences between pass-by-value and pass-by-reference in c++ 区分传递参考和传递价值 - Distinguishing Pass-by-Reference and Pass-by-Value 我可以让C ++编译器决定是按值传递还是按引用传递? - Can I let the C++ compiler decide whether to pass-by-value or pass-by-reference? C ++:使用&运算符传递引用 - C++: using & operator for pass-by-reference 当C ++ / 11 auto关键字更有效或总是按值传递时,它是否会推导出参数传递引用? - Does C++/11 auto keyword deduce to parameters to pass-by-reference when that is more efficient, or always pass-by-value? 我应该在哪里更喜欢按引用传递或按值传递? - Where should I prefer pass-by-reference or pass-by-value? 通过查看装配将值传递与引用传递性能进行比较 - Comparing pass-by-value with pass-by-reference performance by looking at assembly 区分函数模板中的值传递和引用传递 - Distinguish between pass-by-value and pass-by-reference in a function template 函数参数按值传递比按引用传递更快? - Function argument pass-by-value faster than pass-by-reference? 值传递和 std::move 优于传递引用的优点 - Advantages of pass-by-value and std::move over pass-by-reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM