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,
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 :
So, my questions are:
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.
You know the standards better than I, but going on the information you've provided, the standard defines a trivial destructor, but it doesn't define an empty destructor, which would make this question kind of misleading. A trivial destructor is then a special case that compilers can optimize to, and while an empty constructor makes sense to us, it's not something compiler writers have to consider.
Browsing a few SO links:
To answer your second question, as soon as your ctor is non-empty, it isn't trivial. The closest you get to trivial is an empty ctor/dtor, and your careful reading of the standard already tells you that that's not defined to be trivial.
TL;DR: The standard defines a trivial dtor, but not an empty one. Smart compilers can choose to notice that it's user-defined empty and treat it as trivial, but the standard doesn't require any such consideration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With