Kevin eyeson
Kevin eyeson

Reputation: 363

=default and =delete is a function declaration or a function definition?

In cppreference, function declaration, under the section User-provided functions. There is a sentence:

Declaring a function as defaulted after its first declaration can provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base.

The main question is =default and =delete is a function declaration or a function definition?

Shoudn't that be

Defining a function as defaulted after its first declaration?

Upvotes: 4

Views: 245

Answers (2)

dfrib
dfrib

Reputation: 73186

The main question is =default and =delete is a function declaration or a function definition?

As per [dcl.fct.def.delete]/1 and [dcl.fct.def.default]/1 they are definitions. However definitions are also declarations whilst not all declarations are definitions.

Shouldn't that be

Defining a function as defaulted after its first declaration?

As per above both 'declaring' and 'defining' are formally correct, but indeed, using 'defining' could arguably be more non-formally precise.

Finally note that there is key difference between explicitly deleted and explicitly defaulted functions in the context of this question: a deleted function shall be the first declaration of the function (except for deleting explicit specializations of function templates - deletion should be at the first declaration of the specialization), meaning you cannot declare a function and later delete it, say, at its definition local to a translation unit; as per [dcl.fct.def.delete]/4:

A deleted function is implicitly an inline function ([dcl.inline]).

[Note 2: The one-definition rule ([basic.def.odr]) applies to deleted definitions. — end note]

A deleted definition of a function shall be the first declaration of the function or, for an explicit specialization of a function template, the first declaration of that specialization. An implicitly declared allocation or deallocation function ([basic.stc.dynamic]) shall not be defined as deleted.

[Example 4:

struct sometype {
  sometype();
};
sometype::sometype() = delete;  // error: not first declaration

— end example]

Upvotes: 1

HolyBlackCat
HolyBlackCat

Reputation: 96316

Those are definitions. But like most (all?) definitions, they're also declarations.

[dcl.fct.def.delete]/1:

A deleted definition of a function is a function definition whose ...

[dcl.fct.def.default]/1:

A function definition whose function-body is of the form = default ; is called an explicitly-defaulted definition ...

Upvotes: 1

Related Questions