Trivial Class vs Aggregate Structure

Trivial Class

A trivial class is a class that:

  1. Has a trivial default constructor.
  2. Has a trivial copy constructor.
  3. Has a trivial move constructor (since C++11).
  4. Has a trivial copy assignment operator.
  5. Has a trivial move assignment operator (since C++11).
  6. Has a trivial destructor.
  7. Has no virtual functions or virtual base classes.

The trivial constructors/operations/destructor means they are not user-provided (i.e., is implicitly-defined or defaulted on its first declaration).

Trivial classes are basically those that allow bit-wise copy. The operations on these classes are expected to be fast because they can be replaced with simple memory operations like memcpy or memcmp.

Aggregate Structure

An aggregate is arrays or a class (typically a struct, but possibly a class as well) that:

  1. Has no user-declared constructors.
  2. Has no private or protected non-static data members.
  3. Has no virtual functions.
  4. Has no virtual, private, or protected base classes.
  5. Has no user-defined destructor.
PropertyTrivial ClassAggregate StructureNotes
User-Declared ConstructorsAllowedNot AllowedAggregate cannot have user-declared constructors.
Private/Protected Data MembersAllowedNot AllowedAggregates can’t have private or protected non-static members.
Virtual FunctionsNot AllowedNot AllowedNeither can have virtual functions.
Virtual, Private, Protected BasesNot AllowedNot AllowedNeither can have these types of base classes.
User-Defined DestructorAllowedNot AllowedAggregate cannot have a user-defined destructor.
Bitwise Copy-ableYesYesBoth can usually be copied using memcpy.
Aggregate InitializationSometimesYesOnly if the trivial class is also an aggregate.
Allows OptimizationsYesYesBoth allow for various compiler optimizations.

Reference

https://en.cppreference.com/w/cpp/language/classes

https://en.cppreference.com/w/cpp/language/aggregate_initialization