The C++ standard defines some very specific behaviors when a class has a trivial constructor and/or a trivial destructor.
As an example, as per §3.8/1 of the standard:
The lifetime of an object of type
T
ends when:— if
T
is a class type with a non-trivial destructor (12.4), the destructor call starts, or— the storage which the object occupies is reused or released.
So,
- if an object is not trivialy destructible, any attempt to access members of the object after the destructor is called is UB.
- if an object is trivialy destructible, attempt to access members of the object after the destructor is called is safe and not UB.
Although this example may not be the best one, it shows that the difference in behavior maybe crucial (UB/non-UB) whether an object is trivialy destructible or not.
§12.4/3 of the Standard states that (to sumerize) a destructor of a class T
is trivial if it is implicitely defined, not virtual, and if all base classes and members of class T
are trivially destructible.
In my (modest) experience, I never saw any difference, in terms of code generated by the compiler, between :
- a class with a trivial default ctor and/or a trivial dtor, and
- a class with a user defined empty ctor and/or a non-virtual user defined empty dtor (as long as the class, its base classes and members classes also have non-virtual dtor user defined empty or trivial)
So, my questions are:
- In what way a user defined empty ctor/dtor can or cannot be considered as a trivial-like ctor/dtor regarding compiler code generation, optimizations, trade-offs, ...
- Same question with user defined non-empty ctor/dtor; what rules should follow a code implemented in ctor/dtor to consider them as trivial-like.
My question is not related to standard (please, do not answer the standard states what is a trivial ctor/dtor, so user defined ctor/dtor is not) but to the way compilers deal with user defined ctor/dtor and in what way the behavior of a compiled code may change (or not) compared to trivial ctor/dtor.