[c++] What are Aggregates and PODs and how/why are they special?

What has changed for C++14

We can refer to the Draft C++14 standard for reference.

Aggregates

This is covered in section 8.5.1 Aggregates which gives us the following definition:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

The only change is now adding in-class member initializers does not make a class a non-aggregate. So the following example from C++11 aggregate initialization for classes with member in-place initializers:

struct A
{
  int a = 3;
  int b = 3;
};

was not an aggregate in C++11 but it is in C++14. This change is covered in N3605: Member initializers and aggregates, which has the following abstract:

Bjarne Stroustrup and Richard Smith raised an issue about aggregate initialization and member-initializers not working together. This paper proposes to fix the issue by adopting Smith's proposed wording that removes a restriction that aggregates can't have member-initializers.

POD stays the same

The definition for POD(plain old data) struct is covered in section 9 Classes which says:

A POD struct110 is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types). Similarly, a POD union is a union that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types). A POD class is a class that is either a POD struct or a POD union.

which is the same wording as C++11.

Standard-Layout Changes for C++14

As noted in the comments pod relies on the definition of standard-layout and that did change for C++14 but this was via defect reports that were applied to C++14 after the fact.

There were three DRs:

So standard-layout went from this Pre C++14:

A standard-layout class is a class that:

  • (7.1) has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • (7.2) has no virtual functions ([class.virtual]) and no virtual base classes ([class.mi]),
  • (7.3) has the same access control (Clause [class.access]) for all non-static data members,
  • (7.4) has no non-standard-layout base classes,
  • (7.5) either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • (7.6) has no base classes of the same type as the first non-static data member.109

To this in C++14:

A class S is a standard-layout class if it:

  • (3.1) has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • (3.2) has no virtual functions and no virtual base classes,
  • (3.3) has the same access control for all non-static data members,
  • (3.4) has no non-standard-layout base classes,
  • (3.5) has at most one base class subobject of any given type,
  • (3.6) has all non-static data members and bit-fields in the class and its base classes first declared in the same class, and
  • (3.7) has no element of the set M(S) of types as a base class, where for any type X, M(X) is defined as follows.104 [ Note: M(X) is the set of the types of all non-base-class subobjects that may be at a zero offset in X. — end note ]
    • (3.7.1) If X is a non-union class type with no (possibly inherited) non-static data members, the set M(X) is empty.
    • (3.7.2) If X is a non-union class type with a non-static data member of type X0 that is either of zero size or is the first non-static data member of X (where said member may be an anonymous union), the set M(X) consists of X0 and the elements of M(X0).
    • (3.7.3) If X is a union type, the set M(X) is the union of all M(Ui) and the set containing all Ui, where each Ui is the type of the ith non-static data member of X.
    • (3.7.4) If X is an array type with element type Xe, the set M(X) consists of Xe and the elements of M(Xe).
    • (3.7.5) If X is a non-class, non-array type, the set M(X) is empty.

Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to c++11

Remove from the beginning of std::vector Converting std::__cxx11::string to std::string What exactly is std::atomic? C++ How do I convert a std::chrono::time_point to long and back Passing capturing lambda as function pointer undefined reference to 'std::cout' Is it possible to use std::string in a constexpr? How does #include <bits/stdc++.h> work in C++? error::make_unique is not a member of ‘std’ no match for ‘operator<<’ in ‘std::operator

Examples related to aggregate

Pandas group-by and sum SELECT list is not in GROUP BY clause and contains nonaggregated column Aggregate multiple columns at once Pandas sum by groupby, but exclude certain columns Extract the maximum value within each group in a dataframe How to group dataframe rows into list in pandas groupby Mean per group in a data.frame Summarizing multiple columns with dplyr? data.frame Group By column Compute mean and standard deviation by group for multiple variables in a data.frame

Examples related to c++17

How to enable C++17 compiling in Visual Studio? What are the new features in C++17? enum to string in modern C++11 / C++14 / C++17 and future C++20 Iterator invalidation rules What are Aggregates and PODs and how/why are they special?

Examples related to standard-layout

What are Aggregates and PODs and how/why are they special?