编译器将不定义隐式的条件(构造函数,析构函数,副本构造函数,副本分配) [英] Conditions under which compiler will not define implicits (constructor, destructor, copy constructor, copy assignment)

查看:115
本文介绍了编译器将不定义隐式的条件(构造函数,析构函数,副本构造函数,副本分配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个琐碎的问题,但是我在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.

  1. 默认(无参数)构造函数
  2. 复制构造函数
  3. 复制分配运算符
  4. 析构函数

但是我读过某处(我现在似乎找不到),在某些情况下编译器不会隐式实现它们.

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而言,是一个采用XX&const X&的构造函数)
  • 存在一个不可复制构造的数据成员(例如,没有复制构造函数或不可访问的复制构造函数的类)
  • 基类不可复制构造
  • (C ++ 11)您已声明了移动构造函数或移动赋值运算符
  • (C ++ 11)您已明确告诉编译器不要使用X(const X&) = delete;
  • 生成一个
  • you have explicitly declared a copy constructor (for class X a constructor taking X, X& or const 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:

  • 您已经明确声明了一个副本分配运算符(对于类Xoperator=带有XX&const X&)
  • 您的类中有一个不可复制分配的数据成员(例如引用,const对象或没有或无法访问赋值运算符的类)
  • 基类不可复制分配
  • (C ++ 11)您已声明了移动构造函数或移动赋值运算符
  • (C ++ 11)您已明确告诉编译器不要使用X& operator=(const X&) = delete;
  • 生成一个
  • you have explicitly declared a copy assignment operator (for class X an operator= taking X, X& or const 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 taking X&&)
  • 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, an operator= taking X&&)
  • 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屋!

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