删除复制构造函数和复制赋值运算符。其中哪些是必不可少的?
Deleting copy constructors and copy assignment operators. Which of them are essential?
我有一个用例,即不得以任何方式复制我的对象。我在下面写了一个夸张的复制构造函数和复制赋值运算符删除的完整列表。它们太多了,我无法确定使用哪些,有时这让我变得偏执。我不必将它们全部写在我的代码中,对吗?那么,为了防止任何类型的对象复制,我应该使用其中的哪一个?
MyClass ( MyClass &) = delete;
MyClass (const MyClass &) = delete;
MyClass ( MyClass &&) = delete;
MyClass (const MyClass &&) = delete;
MyClass operator=( MyClass &) = delete;
MyClass operator=(const MyClass &) = delete;
const MyClass operator=( MyClass &) = delete;
const MyClass operator=(const MyClass &) = delete;
MyClass & operator=( MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
const MyClass & operator=( MyClass &) = delete;
const MyClass & operator=(const MyClass &) = delete;
MyClass && operator=( MyClass &) = delete;
MyClass && operator=(const MyClass &) = delete;
const MyClass && operator=( MyClass &) = delete;
const MyClass && operator=(const MyClass &) = delete;
MyClass operator=( MyClass &&) = delete;
MyClass operator=(const MyClass &&) = delete;
const MyClass operator=( MyClass &&) = delete;
const MyClass operator=(const MyClass &&) = delete;
MyClass & operator=( MyClass &&) = delete;
MyClass & operator=(const MyClass &&) = delete;
const MyClass & operator=( MyClass &&) = delete;
const MyClass & operator=(const MyClass &&) = delete;
MyClass && operator=( MyClass &&) = delete;
MyClass && operator=(const MyClass &&) = delete;
const MyClass && operator=( MyClass &&) = delete;
const MyClass && operator=(const MyClass &&) = delete;
MyClass (const MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
这些是唯一隐式定义的复制构造函数和复制赋值运算符。
只需要将单个复制构造函数和复制赋值运算符标记为delete
。复制版本的存在将阻止移动构造函数和移动赋值运算符的隐式声明,并且声明一种形式的复制特殊成员函数会抑制其他形式的隐式声明。
MyClass (const MyClass&) = delete;
MyClass& operator= (const MyClass&) = delete;
请注意,post-C++11,默认赋值运算符的隐式定义已弃用,而应将其定义为已删除。
我有一个用例,即不得以任何方式复制我的对象。我在下面写了一个夸张的复制构造函数和复制赋值运算符删除的完整列表。它们太多了,我无法确定使用哪些,有时这让我变得偏执。我不必将它们全部写在我的代码中,对吗?那么,为了防止任何类型的对象复制,我应该使用其中的哪一个?
MyClass ( MyClass &) = delete;
MyClass (const MyClass &) = delete;
MyClass ( MyClass &&) = delete;
MyClass (const MyClass &&) = delete;
MyClass operator=( MyClass &) = delete;
MyClass operator=(const MyClass &) = delete;
const MyClass operator=( MyClass &) = delete;
const MyClass operator=(const MyClass &) = delete;
MyClass & operator=( MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
const MyClass & operator=( MyClass &) = delete;
const MyClass & operator=(const MyClass &) = delete;
MyClass && operator=( MyClass &) = delete;
MyClass && operator=(const MyClass &) = delete;
const MyClass && operator=( MyClass &) = delete;
const MyClass && operator=(const MyClass &) = delete;
MyClass operator=( MyClass &&) = delete;
MyClass operator=(const MyClass &&) = delete;
const MyClass operator=( MyClass &&) = delete;
const MyClass operator=(const MyClass &&) = delete;
MyClass & operator=( MyClass &&) = delete;
MyClass & operator=(const MyClass &&) = delete;
const MyClass & operator=( MyClass &&) = delete;
const MyClass & operator=(const MyClass &&) = delete;
MyClass && operator=( MyClass &&) = delete;
MyClass && operator=(const MyClass &&) = delete;
const MyClass && operator=( MyClass &&) = delete;
const MyClass && operator=(const MyClass &&) = delete;
MyClass (const MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
这些是唯一隐式定义的复制构造函数和复制赋值运算符。
只需要将单个复制构造函数和复制赋值运算符标记为delete
。复制版本的存在将阻止移动构造函数和移动赋值运算符的隐式声明,并且声明一种形式的复制特殊成员函数会抑制其他形式的隐式声明。
MyClass (const MyClass&) = delete;
MyClass& operator= (const MyClass&) = delete;
请注意,post-C++11,默认赋值运算符的隐式定义已弃用,而应将其定义为已删除。