Reputation: 1544
12.1/5 A constructor is trivial if it is an implicitly-declared default constructor and if:
— its class has no virtual functions (10.3) and no virtual base classes (10.1), and
— all the direct base classes of its class have trivial constructors, and
— for all the nonstatic data members of its class that are of class type (or array thereof), each such class has a trivial constructor.
First I thought a trivial constructor is just an implicit default constructor. But when reading the above text in standard, it seems trivial constructor is not only a implicit default constructor but it has other requirements as mentioned above. What does it mean? Whats the point of having a trivial constructor?
For example:
class X
{
// ...
};
Does class X has a trivial or implicit default constructor?
Upvotes: 1
Views: 1196
Reputation: 726539
Trivial constructor does nothing at all. The list from your post says that a constructor is trivial when:
These rules taken together mean that the constructor has nothing to do, hence it is trivial.
In case of X
, it all depends on its data members: if they all have trivial constructors, and if you did not provide a non-trivial constructor yourself, X
will have a trivial constructor too.
Upvotes: 1
Reputation: 183290
It depends what's in the // ...
.
Every trivial constructor is an implicitly-declared default constructor, but not every implicitly-declared default constructor is a trivial constructor. Class X has a trivial destructor if it has an implicit default, and every one of its base-classes has an implicit default (as well as those base-classes' base-classes, and so on), and every one of its members is either a primitive like int
or else is of a type with an implicit default (as well as its members' members, and so on, as well as its members' base-classes, and their base-classes, and so on, as well its base-classes' members, and their members, and so on, and so on, and so on).
It may be easier to look at it the opposite way: if a class doesn't have a trivial constructor, then no class that extends it ("is-a") or includes it ("has-a") has a trivial constructor, either.
Upvotes: 2