Is the copy and swap idiom still useful in C++11

后端 未结 4 801
轮回少年
轮回少年 2020-12-24 14:16

I refer to this question: What is the copy-and-swap idiom?

Effectively, the above answer leads to the following implementation:

class MyClass
{
publi         


        
4条回答
  •  抹茶落季
    2020-12-24 14:53

    The validity of the reasons (if any) to use the copy-and-swap idiom to implement copy assignment are the same in C++11 as they are in previous versions.

    Also note that you should use std::move on member variables in the move constructor, and you should use std::move on any rvalue references that are function parameters.

    std::forward should only be used for template parameter references of the form T&& and also auto&& variables (which both may be subject to reference folding into lvalue references during type deduction) to preserve their rvalueness or lvalueness as appropriate.

提交回复
热议问题