Declaring a function as defaulted after its first declaration

后端 未结 1 1185
醉话见心
醉话见心 2021-02-14 13:32

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

Explicitly-defaulted functions and implicitly-declared functi

1条回答
  •  独厮守ぢ
    2021-02-14 13:52

    Suppose you have

    // A.h
    struct A {
      A();
    };
    

    and

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

    You can change it to

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

    to not force code using A.h to be recompiled.

    For a default constructor, this doesn't make much sense. = default takes up more characters than { }. But think of other constructor types: a copy or move constructor may become much shorter if it is no longer necessary to explicitly mention each field, and depending on the compiler and type you're dealing with, the defaulted copy/move constructor may even perform better, for example if the compiler can only detect that a memcpy call will suffice when you use the = default syntax.

    0 讨论(0)
提交回复
热议问题