默认的默认构造函数 ?在n3290草案中

发布于 2024-12-05 16:26:24 字数 809 浏览 1 评论 0原文

n3290 草案 §12.1(构造函数)中的一点 ¶5:

隐式声明的默认构造函数是以下对象的内联公共成员: 它的阶级。类 X默认默认构造函数被定义为已删除 如果:

  • X 是一个类似联合的类,它有一个带有非平凡成员的变体成员 默认构造函数,
  • 任何没有大括号或等号初始化器的非静态数据成员都是 引用类型,
  • const 限定类型(或数组)的任何非变体非静态数据成员 其中)没有大括号或等于初始化器没有用户提供的 默认构造函数,
  • X 是一个联合体,其所有变体成员都是 const 限定类型 (或其数组),
  • X 是一个非联合类,任何匿名联合成员的所有成员都是 const 限定类型(或其数组),
  • 任何直接或虚拟基类,或不带任何内容的非静态数据成员 大括号或等于初始化器,具有类类型 M (或其数组)并且 M 没有应用默认构造函数或重载解析 (13.3) M 的默认构造函数会导致歧义或导致函数 从默认的默认构造函数中删除或无法访问,或者
  • 任何直接或虚拟基类或非静态数据成员都具有带有
    的类型 已从默认的默认构造函数中删除或无法访问的析构函数

请用一些示例程序解释默认的默认构造函数

A point from n3290 draft §12.1 (Constructors) ¶5:

An implicitly-declared default constructor is an inline public member of
its class. A defaulted default constructor for class X is defined as deleted
if:

  • X is a union-like class that has a variant member with a non-trivial
    default constructor,
  • any non-static data member with no brace-or-equal-initializer is of
    reference type,
  • any non-variant non-static data member of const-qualified type (or array
    thereof) with no brace-or-equal-initializer does not have a user-provided
    default constructor,
  • X is a union and all of its variant members are of const-qualified type
    (or array thereof),
  • X is a non-union class and all members of any anonymous union member are of
    const-qualified type (or array thereof),
  • any direct or virtual base class, or non-static data member with no
    brace-or-equal-initializer, has class type M (or array thereof) and either
    M has no default constructor or overload resolution (13.3) as applied
    to M’s default constructor results in an ambiguity or in a function that is
    deleted or inaccessible from the defaulted default constructor, or
  • any direct or virtual base class or non-static data member has a type with a
    destructor that is deleted or inaccessible from the defaulted default constructor

Please explain the defaulted default constructor with some example program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-12-12 16:26:24

我认为摘自维基百科解释this:

显式默认和删除的特殊成员函数

在 C++03 中,编译器为不提供自身的类提供默认构造函数、复制构造函数、复制赋值运算符 (operator=) 和析构函数。程序员可以通过定义自定义版本来覆盖这些默认值。 C++ 还定义了几个适用于所有类的全局运算符(例如operator= 和operator new),程序员可以重写它们。

但是,对这些默认值的创建几乎没有控制。例如,使类本质上不可复制,需要声明私有复制构造函数和复制赋值运算符,而不是定义它们。尝试使用这些函数违反了单一定义规则。虽然不需要诊断消息,[5] 这通常会导致链接器错误。[需要引用]

对于默认构造函数,如果类定义了任何构造函数,编译器将不会生成默认构造函数。这在许多情况下都很有用,但能够同时拥有专门的构造函数和编译器生成的默认值也很有用。

C++11 将允许显式默认和删除这些特殊成员函数。例如,以下类型显式声明它正在使用默认构造函数:

代码示例:

struct SomeType 
{
    SomeType() = default; //The default constructor is explicitly stated.
    SomeType(OtherType value);
};

由于您似乎是 Standerdese 粉丝(几乎所有问题都寻求标准引用的解释)本文介绍了标准提交如何定义默认值和删除的函数应该值得您阅读:

默认和删除的函数

I think this excerpt from Wikipedia explains this:

Explicitly-defaulted and deleted special member functions

In C++03, the compiler provides, for classes that do not provide for themselves, a default constructor, a copy constructor, a copy assignment operator (operator=), and a destructor. The programmer can override these defaults by defining custom versions. C++ also defines several global operators (such as operator= and operator new) that work on all classes, which the programmer can override.

However, there is very little control over the creation of these defaults. Making a class inherently non-copyable, for example, requires declaring a private copy constructor and copy assignment operator and not defining them. Attempting to use these functions is a violation of the one definition rule. While a diagnostic message is not required,[5] this typically results in a linker error.[citation needed]

In the case of the default constructor, the compiler will not generate a default constructor if a class is defined with any constructors. This is useful in many cases, but it is also useful to be able to have both specialized constructors and the compiler-generated default.

C++11 will allow the explicit defaulting and deleting of these special member functions. For example, the following type explicitly declares that it is using the default constructor:

Code Example:

struct SomeType 
{
    SomeType() = default; //The default constructor is explicitly stated.
    SomeType(OtherType value);
};

Since you seem to be a Standerdese fan(Almost all of your Questions seek explanations on Standard Quotes) this paper here about how standards committe arrive to defining default and deleted functions should be a good read for you:

Defaulted and Deleted Functions

本宫微胖 2024-12-12 16:26:24

如果使用 = default; 语法声明特殊成员函数之一,则该函数是“默认的”。您引用的第一行之前的行指出:

如果类 X 没有用户声明的构造函数,则没有参数的构造函数将隐式声明为默认构造函数 (8.4)。

因此,“默认”默认构造函数是使用 = default 声明的默认构造函数(可以不带参数调用的构造函数)。这可以使用 = default 语法显式定义,也可以根据上面的行隐式定义。

One of the special member functions is "defaulted" if it is declared with the = default; syntax. The line right before the first line you quoted states:

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).

Therefore, a "defaulted" default constructor is a default constructor (constructor that can be called with no arguments) that is declared with = default. This can be explicitly defined using the = default syntax, or implicitly defined, per the above line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文