[英]A defaulted default constructor, why is it not a user-provided default constructor?
For example, clang
does not compile this code, because, the defaulted default constructor for struct A
below, A() = default;
例如, clang
不编译此代码,因为,下面的struct A
的默认默认构造struct A
, A() = default;
is not considered to be user-provided. 不被视为用户提供。
struct A{ A() = default; };
const A a;
But if you look at [dcl.fct.def.general]/1 you'll see: 但如果你看[dcl.fct.def.general] / 1,你会看到:
function-body: 函数体:
ctor-initializer opt compound-statement ctor-initializer opt 复合语句
function-try-block 功能试块
= default ;
= delete ;
That is, = default;
也就是说, = default;
is the function body for the default constructor A::A()
, which is the same as saying that the definition A() = default;
是默认构造函数A::A()
的函数体 ,它与定义A() = default;
above is equivalent to A(){}
as {}
is the body for a default constructor. 上面等价于A(){}
因为{}
是默认构造函数的主体。
By the way, g++
compiles the snippet above, but I know g++
has other issues in this regard, according to this comment by Jonathan Wakely. 顺便说一句, g++
编译上面的代码片段,但我知道g++
在这方面有其他问题,根据Jonathan Wakely的评论 。
Because the standard says so ( [dcl.fct.def.default]/5 ): 因为标准是这样说的( [dcl.fct.def.default] / 5 ):
A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. 如果函数是用户声明的,并且在第一个声明中未明确默认或删除,则用户提供该函数。
Doing it this way allows you to maintain the triviality property with = default;
这样做可以让你用= default;
维护triviality属性= default;
. 。 Otherwise, there's no way to give a class with another constructor a trivial default constructor. 否则,没有办法给另一个构造函数的类一个普通的默认构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.