Home c++ than an empty default designer differs from = default;?

than an empty default designer differs from = default;?

Author

Date

Category

for default designer,
The empty body differs from what happens with = default ?

x :: x () {}
// and
X :: X () = DEFAULT;

Answer 1, Authority 100%

The presence of a default designer with an empty body automatically makes the class nontrivial . With all the features arising from here. For example, makes the type not POD . And also excludes the ability to use aggregate initialization:

# include & lt; iostream & gt;
#Include & lt; Type_traits & gt;
STRUCT X {
  // x () {}
  X () = default;
  int a;
  int b;
};
INT MAIN () {
  X x {1, 2}; // Error, if X is not a trivial class.
  STD :: COUT & LT; & LT; STD :: BOOLALPHA & LT; & LT;
    STD :: IS_TRIVAL & LT; X & GT; :: Value & lt; & lt; "\ n";
}

When determining the designer as = default Class triviality persists if She was before. In this case, it is equivalent to the lack of explicitly mention of the designer in the class definition.

If the default designer is defined as = default Outside the class definition, it will still be considered that the designer is provided by the user, and this is also Makes a class non-trivial .

Footnote on the standard about this (8.4.2 / 5):

A FUNCTION IS User-Provided if it is user-declared and not explicitly
Defaulted or Deleted on Its First Declaration.


The difference will also be observed when trying to create a default constant object object. For example:

const x x;

will lead to error compilation when defining a class X How:

struct x {
  X () = default;
  int i;
};

and compiled successfully In the case of the default designer user:

struct x {
  X () {}
  int i;
};

Another point demonstrating the difference occurs when using an empty initialization list when defining an object. For example:

x x = {};

will reset all members of the class when = default or the missing explicit constructor. If the designer is defined with an empty body, then such a record will leave the class members uninitialized (garbage).


Answer 2, Authority 74%

Definition with = default; , as well as the lack of definition (implicit definition) means that the compiler himself must choose a suitable implementation.
In particular, the compiler can determine the function remote:

struct x {
 int & amp; R;
 // x () {} // Error, "R" is not initialized
 X () = default; // OK, the compiler will generate x () = delete; (SIC!)
         // Reference R does not allow to generate the default designer.
 X (int & amp; r): r (r) {}
};

This is not enough in the usual code, but can come in handy inside the template – depending on the types of class members, the compiler will select whether it is necessary to make the designer or delete it.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions