Back to TILs

C++ aggregate

Date: 2021-07-19Last modified: 2024-01-22

Table of contents

An aggregate is an array or a class with

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

References