Alexander Alexander - 11 months ago 53
C++ Question

A defaulted default constructor, why is it not a user-provided default constructor?

For example,

does not compile this code, because, the defaulted default constructor for
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:


     ctor-initializeropt compound-statement


= default ;

= delete ;

That is,
= default;
is the function body for the default constructor
, which is the same as saying that the definition
A() = default;
above is equivalent to
is the body for a default constructor.

By the way,
compiles the snippet above, but I know
has other issues in this regard, according to this comment by Jonathan Wakely.

Answer Source

Because the standard says so ([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;. Otherwise, there's no way to give a class with another constructor a trivial default constructor.