Home > Enterprise >  =default and =delete is a function declaration or a function definition?
=default and =delete is a function declaration or a function definition?

Time:02-02

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?

CodePudding user response:

In the same sense we can also argue that the term declaring as defaulted should be transformed into defining as defaulted, because when we declare a function as defaulted, it actually generates the definition of the function. So here

struct triv
{
    triv() = default;
};

We at the same time declare it's default constructor, and henceforth define it. I think it is called declaring as defaulted, because of syntax being similar more to a declaration of a function than a definition of a function. Also in C 11 standard, in 8.4.2.4. It is noted that

A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted.

So I think it is redundant to search for a difference in terms defaulting a function, declaring as defaulted and defining as defaulted.

CodePudding user response:

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 ...

  • Related