Declaring a default function after its first declaration

In 8.4.2 Explicitly-defaulted functions [dcl.fct.def.default] standard,

Explicit default functions and implicitly declared functions are collectively referred to as default functions, and the implementation should provide them with implicit definitions (12.1 12.4, 12.8), which may mean their removal. A special member function is a user if it is declared by the user and is not explicitly installed by default or deleted by its first declaration. The function provided by the user is clearly defaulted (i.e., clearly defaulted after the first declaration) defined at the point where it is clearly defaulted; if such a function is implicitly defined as remote, the program is poorly formed. [Note. Declaring a default function after its first declaration can provide efficient execution and a brief definition, allowing a stable binary interface to the evolving code base.-End note]

What does the note at the end mean? From what I see, declaring a default function after its first declaration will make the function user-provided, so make the function non-trivial and thus make the type have a non-trivial default constructor or make the type non-trivial -copyable, and, of course, make the type is non-trivial and non-POD, but still has an implementation to provide the actual definition of a function. But I don’t understand how this leads to " provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base ". Any thoughts are welcome, and real-world examples are much appreciated. Thanks.

An example of this type:

 struct A { A(); }; A::A() = default; 
+7
c ++ c ++ 11
source share
1 answer

Suppose you have

 // Ah struct A { A(); }; 

and

 // A.cc A::A() { } 

You can change it to

 // A.cc A::A() = default; 

so as not to recode the code with Ah .

For the default constructor, this does not make much sense. = default takes up more characters than { } . But think about other types of constructors: the copy or move constructor can become much shorter if you no longer need to explicitly specify each field, and depending on the compiler and the type you are dealing with, the default copy / move constructor may even perform better, for example, if the compiler can only detect that a memcpy call will suffice if you use the syntax = default .

+6
source share

All Articles