In C ++, the following concepts are quite common: trivial class, destructor, constructor. What does the trivial mean from the point of view of the compiler?

  • inline functions are not determined by the volume of the body - VTT
  • 2
    The @BadCatss inline , first and foremost, a mechanism for writing code so that the compiler has the ability to embed it. The recommendation to do this is a secondary effect. From the point of view of the language, the function is or is not inline regardless of whether the embedding actually occurred or not ... - Fat-Zer
  • one
    All these definitions are in the standard (for example, the trivial copy constructor ). In one place, much has been gathered here in the section "Triviality" - cpp questions
  • one
    @BadCatss, yes, if you write the inline before declaring a function (or if it is implied), then from the point of view of the language, it will, surprise, be an inline function . By “ability to embed” I mean that classical C / C ++ compilers can in principle embed a function only defined within the same translation unit ... inline allows the programmer to define it in this way (read in the header) and avoid duplication of both the object code and errors of multiple determination with the line. - Fat-Zer
  • one
    And yes, the compiler has the right to embed any function without disturbing the result of the calculations, depending on its clever heuristics, bugs, compilation keys or something more interesting ... - Fat-Zer

1 answer 1

It is correct to ask: what does the trivial mean (class, constructor, destructor, etc.) from the point of view of the standard, and not the compiler?

This is an extensive question (you need to go through all the entities that are called trivial) so you should independently refer to the C ++ standard.

For example, according to the C ++ 17 standard (12 Classes) a class is called trivial if it is trivially copied.

6 A trivially copyable class is a class:

(6.1) - where each copy constructor, move constructor, copy assignment operator, move move assignment operator (15.8, 16.5.3) is either deleted or trivial,

(6.2) - it is a non-deleted copy constructor, one constructor, one assignment operator, or one

(6.3) - that has a trivial, non-deleted destructor (15.4).

It is a trivially constructive copyright pattern (15.1), [Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes. - end note]

To determine the triviality of constructors and destructors, you should first refer to the next section of the standard (11.4.2 Explicitly-defaulted functions)

  1. ... it is a statement.

6 [Example:

  struct trivial { trivial() = default; trivial(const trivial&) = default; trivial(trivial&&) = default; trivial& operator=(const trivial&) = default; trivial& operator=(trivial&&) = default; ~trivial() = default; }; struct nontrivial1 { nontrivial1(); }; nontrivial1::nontrivial1() = default; // not first declaration 

- end example]

That is, a user-defined constructor or destructor cannot be trivial. Otherwise, the constructor or destructor is trivial if the following additional conditions are met.

(C ++ 17, 15.1 Constructors):

6 A default constructor is if it is not user-provided and if:

(6.1) - its class has no virtual functions (13.3) and no virtual base classes (13.1), and

(6.2) - has no default member initializer (12.2), and

(6.3) - trivial default constructors, and

(6.4) - This is a type of (or array thereof), which is a trivial default constructor.

Otherwise, the default constructor is non-trivial

(C ++ 17, 15.4 Destructors)

6 A destructor is if it is not user-provided and if:

(6.1) - the destructor is not virtual,

(6.2) - trivial destructors, and

(6.3) - This is a type of (or array thereof) for each of the non-static data.

Otherwise, the destructor is non-trivial