编译器将不定义隐式的条件(构造函数,析构函数,副本构造函数,副本分配) [英] Conditions under which compiler will not define implicits (constructor, destructor, copy constructor, copy assignment)
问题描述
这应该是一个琐碎的问题,但是我在stackoverflow上找不到它.
This is supposed to be a trivial question but I could not find it explicitly on stackoverflow.
如果用户未提供以下内容,则会对其进行隐式定义.
The following will be defined implicitly if not provided by the user.
- 默认(无参数)构造函数
- 复制构造函数
- 复制分配运算符
- 析构函数
但是我读过某处(我现在似乎找不到),在某些情况下编译器不会隐式实现它们.
But I have read somewhere (which I cant seem to find now), that there are some conditions where the compiler will not implicitly implement them.
这些条件是什么?
推荐答案
如果满足以下条件,则不会隐式生成默认构造器 (例如,X()
):
- 您已经明确声明了任何构造函数
- 存在一个不可默认构造的数据成员(例如引用,
const
对象或没有默认构造函数或无法访问的默认构造函数的类) - (C ++ 11)您已明确告诉编译器不要使用
X() = delete;
生成一个
- you have explicitly declared any constructor at all
- there is a data member that is not default-constructible (such as a reference, a
const
object, or a class with no or inaccessible default constructor) - (C++11) you have explicitly told the compiler to not generate one using
X() = delete;
副本构造器 (例如,X(const X&)
)在以下情况下不会隐式生成:
The Copy Constructor (e.g., X(const X&)
) will not be implicitly generated if:
- 您已明确声明了一个复制构造函数(对于类
X
而言,是一个采用X
,X&
或const X&
的构造函数) - 存在一个不可复制构造的数据成员(例如,没有复制构造函数或不可访问的复制构造函数的类)
- 基类不可复制构造
- (C ++ 11)您已声明了移动构造函数或移动赋值运算符
- (C ++ 11)您已明确告诉编译器不要使用
X(const X&) = delete;
生成一个
- you have explicitly declared a copy constructor (for class
X
a constructor takingX
,X&
orconst X&
) - there is a data member that is not copy-constructible (such as a class with no or inaccessible copy constructor)
- the base class is not copy-constructible
- (C++11) you have declared a move constructor or move assignment operator
- (C++11) you have explicitly told the compiler to not generate one using
X(const X&) = delete;
副本分配运算符 (例如, X& operator=(const X&)
)在以下情况下不会隐式生成:
The Copy Assignment Operator (e.g., X& operator=(const X&)
) will not be implicitly generated if:
- 您已经明确声明了一个副本分配运算符(对于类
X
和operator=
带有X
,X&
或const X&
) - 您的类中有一个不可复制分配的数据成员(例如引用,
const
对象或没有或无法访问赋值运算符的类) - 基类不可复制分配
- (C ++ 11)您已声明了移动构造函数或移动赋值运算符
- (C ++ 11)您已明确告诉编译器不要使用
X& operator=(const X&) = delete;
生成一个
- you have explicitly declared a copy assignment operator (for class
X
anoperator=
takingX
,X&
orconst X&
) - there is a data member in your class that is not copy-assignable (such as a reference, a
const
object, or a class with no or inaccessible assignment operator) - the base class is not copy-assignable
- (C++11) you have declared a move constructor or move assignment operator
- (C++11) you have explicitly told the compiler to not generate one using
X& operator=(const X&) = delete;
析构函数 (例如,
The Destructor (e.g., ~X()
) will not be implicitly generated if:
- 您已明确声明了一个析构函数
- (C ++ 11)您已明确告诉编译器不要使用
~X() = delete;
生成一个
- you have explicitly declared a destructor
- (C++11) you have explicitly told the compiler to not generate one using
~X() = delete;
移动构造器 (C ++ 11)(例如X(X&&)
)在以下情况下不会隐式生成:
The Move Constructor (C++11) (e.g., X(X&&)
) will not be implicitly generated if:
- 您已明确声明了一个移动构造函数(对于类
X
,采用X&&
的构造函数) - 您已声明了副本分配运算符,副本构造函数,析构函数或移动赋值运算符
- 您的类中有一个不能进行移动构造的数据成员(是
const
,是引用,或者具有已删除,不可访问或模棱两可的移动构造函数) - 基类不能移动构造
- 您已明确告诉编译器不要使用
X(X&&) = delete;
生成一个
- you have explicitly declared a move constructor (for class
X
, a constructor takingX&&
) - you have declared a copy assignment operator, copy constructor, destructor, or move assignment operator
- there is a data member in your class that cannot be move-constructed (is
const
, is a reference, or has a deleted, inaccessible, or ambiguous move constructor) - the base class cannot be move-constructed
- you have explicitly told the compiler to not generate one using
X(X&&) = delete;
移动分配运算符 (C + +11)(例如X& operator=(X&&)
)在以下情况下不会隐式生成:
The Move Assignment Operator (C++11) (e.g., X& operator=(X&&)
) will not be implicitly generated if:
- 您已经明确声明了一个移动分配运算符(对于类
X
,是一个operator=
接受了X&&
) - 您已声明了副本分配运算符,副本构造函数,析构函数或移动构造函数
- 您的班级中有一个数据成员无法移动分配(是
const
,是引用或具有已删除,不可访问或模棱两可的移动分配操作符) - 基类不能移动分配
- 您已明确告诉编译器不要使用
X& operator=(X&&) = delete;
生成一个
- you have explicitly declared a move assignment operator (for class
X
, anoperator=
takingX&&
) - you have declared a copy assignment operator, copy constructor, destructor, or move constructor
- there is a data member in your class that cannot be move-assigned (is
const
, is a reference, or has a deleted, inaccessible, or ambiguous move assignment operator) - the base class cannot be move-assigned
- you have explicitly told the compiler to not generate one using
X& operator=(X&&) = delete;
这篇关于编译器将不定义隐式的条件(构造函数,析构函数,副本构造函数,副本分配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!