编译器何时提供类的特殊成员的定义? [英] When does the compiler provide definitions for the special members of a class?

查看:206
本文介绍了编译器何时提供类的特殊成员的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当我定义一个空类并且根本不提供声明时,编译器将提供默认和拷贝构造函数,析构函数和拷贝赋值运算符的定义。



这些规则是什么?编译器不是提供一个(例如)复制构造函数?移动构造函数和移动赋值运算符呢?



(例如:如果我的类有一个引用成员<$ c $否则会发生这种情况?)

解决方案

在C ++ 11中,它比隐式声明或更复杂。它们可以是隐式声明和默认,隐式声明和删除,或未声明。请阅读以区分后者2.以下信息不完全正确,因为它不区分已声明和已删除的vs未申报。



以下是正在进行的工作。 (?)表示我想澄清或量化声明。



特殊成员职能§12 / 1



当用户没有明确声明它们时,实现将隐式声明一些类类型的这些成员函数:




  • 默认构造函数

  • 复制构造函数

  • 复制赋值运算符

  • 移动构造函数


  • 移动赋值运算符

  • $ b

    如果类型具有...则不会隐式声明特殊成员函数。






    默认构造函数§12.1/ 5






    复制构造函数§12.8/ 8, 12 >


    • 用户声明的移动构造函数

    • / li>
    • 右值引用类型的非静态数据成员

    • 变量成员具有非平凡拷贝构造函数并且是类似于类的类/ li>
    • 无法复制的类型(或其数组)的非静态数据成员

    • 无可访问的复制构造函数的直接或虚拟基类



    *如果类有用户声明的复制赋值运算符或用户声明的析构函数




    复制作业操作员§12.8/ 19,§12.8/ 24




    • 用户声明的移动构造函数

    • 用户声明的移动赋值操作符

    • 一个非平凡的副本赋值运算符,并且是类似union的类(?)

    • const非类型(或其数组)的非静态数据成员

    • 引用类型的非静态数据成员

    • 具有不可访问的副本赋值运算符的非静态数据成员

    • 直接或虚拟基类与不可访问的副本赋值运算符



    *如果类具有用户声明的复制构造函数或用户声明的析构函数(?)






    移动构造函数§12.8/ 10,§12.8/ 12




    • 用户声明的复制构造函数

    • 用户声明的复制赋值操作符

    • <移动构造函数不会被隐式定义为已删除(?)用户声明的移动赋值操作符
      <
    • 具有非平凡移动构造函数的变量成员,并且是类(或其数组)的非静态数据成员的类似联合的类(?)

    • 无法移动

    • 非静态数据成员或类型没有移动构造函数且不可复制的直接或虚拟基类

    • 无可移动构造函数的直接或虚拟基类






    §12.8/ 21,§12.8/ 24




    • 用户声明的复制构造函数

    • 用户声明的移动构造函数

    • 用户声明的复制赋值操作符

    • 用户声明的析构函数

    • 赋值运算符不会隐式定义为已删除(?)

    • 一个具有非平凡移动赋值运算符的变量成员,并且是类似于union的类(?)

    • const非类型(或其数组)的非静态数据成员

    • 引用类型的非静态数据成员


    • 不可访问的移动赋值运算符的非静态数据成员,并且不可复制






    析构函数§12.4/ 4


    I know that when I define an empty class and provide no declarations at all, the compiler will provide definitions for the default and copy constructor, destructor and copy assignment operator.

    What are the rules for that? When does the compiler not provide a, say, copy constructor? What about the move constructor and move assignment operator?

    (Example: The compiler will not provide definitions for any assignment operator if my class has a reference member like int&. When else will something like this happen?)

    解决方案

    Edit: In C++11 it's more complicated than implicitly declared or not. They can either be implicitly declared and defaulted, implicitly declared and deleted, or undeclared. Read this to distinguish the latter 2. The following information isn't entirely correct since it doesn't distinguish declared and deleted vs undeclared.

    The following is a work in progress. (?) indicates I would like to clarify or quantify the statement.

    Special Member Functions §12/1

    The implementation will implicitly declare these member functions for some class types when the user does not explicitly declare them:

    • default constructor
    • copy constructor
    • copy assignment operator
    • move constructor
    • move assignment operator
    • destructor

    A special member function will NOT be implicitly declared if the type has a...


    Default Constructor §12.1/5


    Copy Constructor §12.8/8, §12.8/12

    • user-declared move constructor
    • user-declared move assignment operator
    • non-static data member of rvalue reference type
    • variant member with a non-trivial copy constructor and is a union-like class (?)
    • non-static data member of type (or array thereof) that cannot be copied
    • direct or virtual base class without accessible copy constructor

    *Such an implicit declaration is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor (?)


    Copy Assignment Operator §12.8/19, §12.8/24

    • user-declared move constructor
    • user-declared move assignment operator
    • a variant member with a non-trivial copy assignment operator and is a union-like class (?)
    • non-static data member of const non-class type (or array thereof)
    • non-static data member of reference type
    • non-static data member with inaccessible copy assignment operator
    • direct or virtual base class with inaccessible copy assignment operator

    *Such implicit declaration is deprecated if the class has a user-declared copy constructor or a user-declared destructor (?)


    Move Constructor §12.8/10, §12.8/12

    • user-declared copy constructor
    • user-declared copy assignment operator
    • user-declared move assignment operator
    • user-declared destructor
    • move constructor would not be implicitly defined as deleted (?)
    • variant member with a non-trivial move constructor and is a union-like class (?)
    • non-static data member of type (or array thereof) that cannot be moved
    • non-static data member or direct or virtual base class with a type that does not have a move constructor and is not trivially copyable
    • direct or virtual base class without accessible move constructor

    Move Assignment Operator §12.8/21, §12.8/24

    • user-declared copy constructor
    • user-declared move constructor
    • user-declared copy assignment operator
    • user-declared destructor
    • move assignment operator would not be implicitly defined as deleted (?)
    • a variant member with a non-trivial move assignment operator and is a union-like class (?)
    • non-static data member of const non-class type (or array thereof)
    • non-static data member of reference type
    • non-static data member with inaccessible move assignment operator and is not trivially copyable
    • direct or virtual base class with inaccessible move assignment operator and is not trivially copyable

    Destructor §12.4/4

    这篇关于编译器何时提供类的特殊成员的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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