Destructors: triviality and implicit definition

As I understand the standard, a trivial destructor is one that is implicitly declared, and whose class has only basic and non-static members with trivial destructors. Given the recursiveness of this definition, it seems to me that the only “recursion-stop” condition is to search for a base or non-static member with an implicit declared destructor (that is, a declared user). If this is correct, this should mean that the trivial destructor is one that "should not do anything," and therefore it will be declared (implicitly) but not defined. To put it another way: is it right to say that an implicitly defined destructor (that is, "it does something") cannot be trivial in accordance with the standard definition?

Sorry for the stupid question, but I would like to clarify the situation in my head a bit ...

+4
source share
3 answers

No. An implicitly defined trivial destructor is by definition trivial. The difference between a declaration and a thingy definition is that in order for the compiler to even see that the destructor is available, there must always be an declaration. Therefore, if you do not provide it, it will implicitly provide it.

But now it will also determine one, if necessary (if an object of this type of class is destroyed). In any case, he must do something: he needs to call the destructors of all its members and base classes. A simple example illustrating the effect of implicitly defining a destructor:

struct a { private: ~a(); }; struct bug { // note: can't be destructed a a_; }; 

As soon as you try to create a local object with an error, the compiler will signal an error, because it gives a definition of the destructor for the error, which tries to call an inaccessible destructor.

Now I think that the triviality of destructors / constructors is mainly used to limit the restrictions on your program. For example, objects that have non-trivial versions cannot be placed in unions. On the other hand, you can delete an object with an incomplete type if it has a trivial destructor. Note: if your program cannot decide if a trivial destructor was really defined, the compiler may omit its definition. This is the so-called as-if rule. The compiler should behave like this: if it is compatible with the standard, optimization does not matter until they change the value of the program.

+6
source

Your wording is a bit unsuccessful. For instance. recursion, of course, also ends when you run out of members and base classes. These wording problems also seem more confusing to you.

In any case, all implicitly declared destructors, regardless of whether they are trivial or not, are defined if and only if they are used. The specific term is used here. A type T destructor is used whenever the lifetime of an object T ends.

Trivial destructors exist because C programmers put structures into unions. This code must be legal in C ++, so the notion of a trivial destructor was invented for C ++. All C structures have trivial destructors compiled as C ++.

+3
source

Consider these two classes:

 class A { }; class B { private: A obj; }; 

Destructors of both of these classes are implicitly defined. However, at the same time, both of them are trivial by standard definitions.

0
source

All Articles