devxlogo

Trivial Constructors

Trivial Constructors

Conceptually, compilers synthesize a default constructor for every class or struct, unless a constructor was already defined by the user. However, in certain conditions (a simple data struct, for example), a synthesized constructor is completely redundant. Compilers avoid synthesizing a default constructor for a class/struct when all the following conditions hold:

  1. The class/struct is not derived from an inheritance chain in which one or more virtual base classes exist.
  2. The class/struct does not define or inherit any virtual member function.
  3. The class/struct does not embed a member that has a default constructor or a virtual function.

By eliminating unnecessary constructor generation, compilers can produce code that is as efficient in terms of size and speed as a C compiler would.

devx-admin

Share the Post: