Alexander
Alexander

Reputation: 2591

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; 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:

function-body:
     ctor-initializeropt compound-statement
     function-try-block
    = default ;
    = delete ;

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

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.

Upvotes: 5

Views: 813

Answers (1)

T.C.
T.C.

Reputation: 137310

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.

Upvotes: 8

Related Questions