gpt4 book ai didi

c++ - 为什么不为将析构函数定义为 =default 的类合成移动操作?

转载 作者:可可西里 更新时间:2023-11-01 18:35:38 24 4
gpt4 key购买 nike

编译器不应为声明析构函数或复制操作(即复制构造函数/赋值)的类合成移动操作的规则是有道理的。毕竟,通过声明这些操作,类承认它需要做一些自定义簿记。

但是,当类将析构函数或复制操作定义为 =default 时,此推理不适用?那么这种情况不应该是规则的异常(exception)吗?

编辑:我可能想将析构函数定义为 =default 而不是其他特殊操作的一个原因是当我需要基类的虚拟析构函数时,所以我不得不定义一个使其虚拟化。

最佳答案

N3174似乎是引入规则的提案 user-declared dtor/copy op => no implicit move。在本文中,Bjarne Stroustrup 对某些类不变量表示关注,如:

class Vec {
vector<int> v;
int my_int; // the index of my favorite int stored in v
// implicit/generated copy, move, and destructor
// ...
};

Vec has what I call an implicit invariant: There is a relation between two objects (here, the members) that is nowhere stated in declaratively in the code.

请注意,此不变量会被移动操作破坏,如果它们是隐式生成的,并且会更改以前行为良好的代码的含义(在复制右值的 C++03 中),这尤其有害。

由于这些不变量,Stroustrup 提出:

  1. Move and copy are generated by default (if an only if their elements move or copy as currently specified in the FCD [dyp: FCD = final committee draft])
  2. If any move, copy, or destructor is explicitly specified (declared, defined, =default, or =delete) by the user, no copy or move is generated by default.

(后来,他建议只在 2. 中弃用隐式生成拷贝,而不是在 C++11 中完全删除它)

我在 N3174 中找到的为什么默认用户声明的操作包含在第二个要点中的最佳解释是不变量和特殊成员函数之间的关系:

I think that the most worrying cases are equivalent to Vec. For such classes there is an implicit invariant but no “indication” to help the compiler [dyp: discover the existence of an invariant] in the form of a user-specified copy constructor. This kind of example occurs (occurred) when a programmer decided that the default copy operations were correct and then (correctly) decided not to mention them because the default copy operations are superior to user-defined ones (e.g. because of ABI issues). In C++0x, a programmer can be explicit by defaulting copy, but that could be considered undesirably verbose.

所以通过编写Vec如下:

class Vec {
vector<int> v;
int my_int; // the index of my favorite int stored in v
public:
Vec() = default;
Vec(Vec const&) = default;
Vec& operator=(Vec const&) = default;
~Vec() = default;
};

我们可以声明默认 复制操作是安全的,而默认移动操作是不安全的。如果用户必须明确地将移动操作定义为已删除,这将“在表达式中留下 [..] 一个漏洞”(参见 N3053:尝试从中复制或返回(非常量)右值将尝试使用删除的移动操作。

为什么 dtor 应该包含在该列表中并不完全明显,但我认为它属于经常出现类不变量的 3/5 特殊成员函数组。

关于c++ - 为什么不为将析构函数定义为 =default 的类合成移动操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31656116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com