C++ aggregate
Date: 2021-07-19Last modified: 2024-11-02
Table of contents
An aggregate is an array or a class with
- no user-declared or inherited constructors,
- no private or protected direct non-static data members,
- no virtual functions, and
- no virtual, private, or protected base classes.
Aggregates can be initialized in aggregate initialization, and, for most cases, decomposed in a structured binding:
struct Point {
int x, y;
}; // aggregate
int main( [[maybe_unused]] int argc, [[maybe_unused]] char **argv )
{
Point pt = {1, 2}; // aggregate init
auto const &[x, y] = pt; // decomposition
cout << "x: " << x << " y: " << y << endl;
return 0;
}
Possible output
x: 1 y: 2