为什么析构函数禁止隐式移动函数的生成 [英] Why destructor disabes generation of implicit move functions

查看:284
本文介绍了为什么析构函数禁止隐式移动函数的生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过阅读其中一个博客 https: //rmf.io/cxx11/rule-of-zero/ ,如果我是正确的,它说如果你声明你自己的dector,那么不要忘记让其余的两个移动ctor /移动分配作为默认。 / p>

示例:从 http://scottmeyers.blogspot.in/2014/03/a-concern-about-rule-of-zero.html

  class Widget {
public:
〜Widget(); // temporary destructor
... //没有复制或移动函数
};




添加析构函数会产生禁用$ b的副作用$ b生成移动函数,但因为Widget是可复制的,所有
用于生成移动的代码现在将生成副本。在
中,换句话说,向类添加析构函数导致$ b $


上面的文本是由Scott提出的里面的引号提出了一些问题在我的心中,为什么声明dector隐藏移动语义?

解决方案

声明/定义只隐藏移动语义或复制ctor / copy赋值零规则实际上是关于别的东西,而不是什么特殊的成员函数被生成和什么时候。这是关于一个一定的态度对类设计。它鼓励您回答一个问题:



我的班级是否管理资源?

>如果是这样,每个资源都应该移动到其专用类,以便您的类只管理资源(并且不做任何其他操作)或只累积其他类和/或执行相同的逻辑任务(但不管理资源)。



这是更一般的单一责任原则的特殊情况。



它,你会立即看到,对于资源管理类,你必须手动移动构造函数,移动分配和析构函数(很少需要复制操作)。对于非资源类,你不需要(事实上你可能不应该)声明任何:move ctor / assignment,copy ctor / assignment,destructor。



因此名称中的零:当你将类分成资源管理和其他类时,在其他你需要提供零特殊的成员函数



在C ++中有一些规则(特殊成员函数的)什么定义禁止什么其他定义,但它们只会分散你对核心的理解



  1. $ b a href =https://akrzemi1.wordpress.com/2015/09/08/special-member-functions/> https://akrzemi1.wordpress.com/2015/09/08/special-member-functions/

  2. https:// akrzemi1.wordpress.com/2015/09/11/declaring-the-move-constructor/


I was trying to understand what the rule of zero says by reading one of the blog https://rmf.io/cxx11/rule-of-zero/ and if i am correct it says if you declare your own dector then don't forget to make the rest two move ctor/move assignment as default.

Example:from http://scottmeyers.blogspot.in/2014/03/a-concern-about-rule-of-zero.html

class Widget {
public:
  ~Widget();         // temporary destructor
  ...                // no copy or move functions
};

"The addition of the destructor has the side effect of disabling generation of the move functions, but because Widget is copyable, all the code that used to generate moves will now generate copies. In other words, adding a destructor to the class has caused presumably-efficient moves to be silently replaced with presumably-less-efficient copies".

The above text by Scott inside the quotes raise some questions in my mind why declaring dector hides the move semantics?? Is declaring/definig dector only hides the move semantics or copy ctor/copy assignment as well hides the move semantics?

解决方案

"The Rule of Zero" is in fact about something else than what special member functions are generated and when. It is about a certain attitude to class design. It encourages you to answer a question:

Does my class manage resources?

If so, each resource should be moved to its dedicated class, so that your classes only manage resources (and do nothing else) or only accumulate other classes and/or perform same logical tasks (but do not manage resources).

It is a special case of a more general Single Responsibility Principle.

When you apply it, you will immediately see that for resource-managing classes you will have to define manually move constructor, move assignment and destructor (rarely will you need the copy operations). And for the non-resource classes, you do not need to (and in fact you probably shouldn't) declare any of: move ctor/assignment, copy ctor/assignment, destructor.

Hence the "zero" in the name: when you separate classes to resource-managing and others, in the "others" you need to provide zero special member functions (they will be correctly auto-generated.

There are rules in C++ what definition (of a special member function) inhibits what other definitions, but they only distract you from understanding the core of the Rule of Zero.

For more information, see:

  1. https://akrzemi1.wordpress.com/2015/09/08/special-member-functions/
  2. https://akrzemi1.wordpress.com/2015/09/11/declaring-the-move-constructor/

这篇关于为什么析构函数禁止隐式移动函数的生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆