c++ - 什么是微不足道的功能?

标签 c++ language-lawyer c++14

[basic.def.odr]/3 引用了术语“非平凡函数”,我在标准 (N4140) 中找不到它的定义。

[basic.def.odr]/3

A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.19) that does not invoke any nontrivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5).

最佳答案

“非平凡函数”是“平凡特殊成员函数”的补充。对于平凡和不平凡的默认/复制/移动构造函数、复制/移动赋值运算符或析构函数有什么定义 - 仅属于特殊成员函数的特征,并决定是否这些需要在某些情况下调用。

这些的定义可以在第 12 章中找到。

默认构造函数,§12.1/4:

A default constructor is trivial if it is not user-provided and if:

  • its class has no virtual functions (10.3) and no virtual base classes (10.1), and
  • no non-static data member of its class has a brace-or-equal-initializer, and
  • all the direct base classes of its class have trivial default constructors, and
  • for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.

Otherwise, the default constructor is non-trivial.

复制/移动构造函数,第 12.8/12 节:

A copy/move constructor for class X is trivial if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list of an implicit declaration, and if

  • class X has no virtual functions (10.3) and no virtual base classes (10.1), and
  • class X has no non-static data members of volatile-qualified type, and
  • the constructor selected to copy/move each direct base class subobject is trivial, and
  • for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;

otherwise the copy/move constructor is non-trivial.

复制/移动赋值运算符,§12.8/26:

A copy/move assignment operator for class X is trivial if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list of an implicit declaration, and if

  • class X has no virtual functions (10.3) and no virtual base classes (10.1), and
  • class X has no non-static data members of volatile-qualified type, and
  • the assignment operator selected to copy/move each direct base class
  • for each non-static data member of X that is of class type (or array thereof), the assignment operator selected to copy/move that member is trivial;

otherwise the copy/move assignment operator is non-trivial.

析构函数,§12.4/5:

A destructor is trivial if it is not user-provided and if:

  • the destructor is not virtual,
  • all of the direct base classes of its class have trivial destructors, and
  • for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

Otherwise, the destructor is non-trivial

关于c++ - 什么是微不足道的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152687/

相关文章:

c++ - 生成随机 uint64_t

c++ - 来自命令行的 gnu 编译器

c++ - Linux C++应用程序中获取SCSI磁盘名称的方法

C++14 使用捕获说明符在 lambda 函数内部递增一个值

c++ - 为什么在其他函数中声明的函数不参与参数相关查找?

c++ - 获取未初始化对象成员的地址是否定义明确?

c++ - 为什么互斥锁和条件变量可以轻松复制?

c++ - CMake 与 Poky 交叉编译

c++ - 如果您不通过引用将对象传递给函数,可能会出现什么问题

c++ - 对于按值传递的重成员,构造函数的初始化列表中真的需要 std::move 吗?