0

Previously:

We know that only special member functions may be defaulted, but any function can be deleted.

N3337:

[dcl.fct.def.default]

A function definition of the form:

             attribute-specifier-seqopt decl-specifier-seqopt declarator = default ;

is called an explicitly-defaulted definition. A function that is explicitly defaulted shall

— be a special member function,

— have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function’s class) as if it had been implicitly declared, and

— not have default arguments.

[dcl.fct.def.delete]

A function definition of the form:

             attribute-specifier-seqopt decl-specifier-seqopt declarator = delete ;

is called a deleted definition. A function with a deleted definition is also called a deleted function.

My question is, why is this so? If the standard can allow any function to be deleted, I don't see the harm in allowing any function to be defaulted. n2210's wording seems to imply that defaulted free functions would be allowed. For example,

Under Changes to the Standard, it says:

[...]

Add a new paragraph 7.

A function definition of "=default;", called an explicitly defaulted definition, has exactly the implementation of an implicit default definition (12.1, 12.4, 12.8). [ Note: Member functions with explicitly defaulted definitions outside the class definition must also be declared within the class definition. — end note ] Unlike with an implicit default definition, the explicitly defaulted definition may be non-inline. [...]

3 years later, N3025 already establishes the language that resembles the final draft, and explicitly restricts defaulted functions to be special member functions. Is there a paper in that 3 year gap or some rationale that explains this?

Community
  • 1
  • 1
  • 1
    Also, -1 for linking questions without reading them. Your first link completely covers the fact that only functions which have defaults can be defaulted. From that question: "I can understand that = default would probably not be allowed outside the special members; since it is basically saying use the compiler generated default. The default would need to be clearly defined before the compiler is able to generate it... only the special members have these pre-defined defaults." – Ben Voigt Dec 01 '14 at 17:34

2 Answers2

4

Because only special member functions have well-defined "default" definitions, generated implicitly if you don't declare the function or any other function that would inhibit it.

If I were to write

int f() = default;

what would you expect the function to do?

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    If I had to answer that, I'd say it throws `std::bad_function_call` :o) +1 – Columbo Dec 01 '14 at 17:33
  • From my experience here on SO, I'd say that `int f() = default;` should define a function that outputs `"Hello, world!"` and returns `42`. I'll make you a co-author on my proposal to add this feature to C++17 ;) – Casey Dec 01 '14 at 17:34
1

The logic hasn't changed at all, only the wording. Look at the original

A function definition of "=default;", called an explicitly defaulted definition, has exactly the implementation of an implicit default definition

Only special member functions have "an implicit default definition". Trying to explicitly default any other function is ill-defined, because it's defined in terms of something that doesn't exist (the implicit default definition).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720