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.

Community
  • 1
  • 1
Alexander
  • 2,581
  • 11
  • 17
  • Strange, as you state, [clang fails](http://coliru.stacked-crooked.com/a/45816775ce659464), [gcc compiles](http://coliru.stacked-crooked.com/a/137a9ea1c836152b). So does VS2015. – wally Oct 22 '16 at 18:23
  • 3
    User-provided explicitly-defaulted would look like `struct A{A();}; A::A()=default;` – David G Oct 22 '16 at 18:34

1 Answers1

8

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.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • I think the term "deleted" on the quote you provided is not necessary, as [[dcl.fct.def.delete]/4](http://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 Oct 23 '16 at 18:11
  • @Alexander that makes `A() = delete;` not user-provided. – T.C. Oct 24 '16 at 14:15
  • 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 Oct 24 '16 at 17:37
  • @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. – T.C. Oct 24 '16 at 20:16