5

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.

2
  • Strange, as you state, clang fails, gcc compiles. So does VS2015. Commented Oct 22, 2016 at 18:23
  • 3
    User-provided explicitly-defaulted would look like struct A{A();}; A::A()=default; Commented Oct 22, 2016 at 18:34

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think the term "deleted" on the quote you provided is not necessary, as [[dcl.fct.def.delete]/4](eel.is/c++draft/dcl.fct.def.delete#4) already prohibits a deleted definition from occurring after the function's first declaration. Am I missing something here?
@Alexander that makes A() = delete; not user-provided.
But at least for the defaulted case we know that the function is either user-provided or implicitly provided by the compiler. But for a function that's explicitly deleted, I'm confused, because, as far as I know, this function is never defined. What would be the purpose of defining such a function, that will never be invoked?
@Alexander I don't get your question. = delete; is a definition. Classifying A() = delete; as not-user-provided doesn't mean you can define it yourself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.