gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:32:41 24 4
gpt4 key购买 nike

n3290 草案 §12.1(构造函数)¶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

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

最佳答案

我觉得 this 维基百科的摘录解释了这一点:

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

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:

代码示例:

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

由于您似乎是 Standerdese 的粉丝(几乎所有问题都寻求对 Standard Quotes 的解释),此处有关标准委员会如何定义默认和已删除函数的论文对您来说应该是一个很好的读物:

Defaulted and Deleted Functions

关于c++ - 默认的默认构造函数?在 n3290 草案中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com