Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Records and aggregates

A record groups named fields, usually in a struct. An aggregate is a formal C++ category that allows element initialization without a user-declared constructor. Not every struct is an aggregate.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

struct Record { int x; int y = 10; };
Record a{3, 4};
Record b{.x = 3};

A record is a descriptive term for a type grouping named fields; C++23 has no separate record keyword. It commonly uses struct, whose members and inheritance are public by default; for class, they are private by default. Choosing struct alone does not guarantee an aggregate.

An aggregate is an array or a class without user-declared or inherited constructors, private or protected direct non-static data members or direct base classes, virtual functions, or virtual base classes. Even an in-class declaration Record() = default; prevents aggregate status in C++23. Ordinary member functions do not prevent it.

Record a{3, 4}; initializes fields in declaration order. In an aggregate with bases, direct bases come first. Braces prohibit narrowing conversions. In C++23, Record b{.x = 7}; designates a direct field; designators must follow declaration order and cannot be mixed with positional initializers.

For a non-union aggregate, an omitted element uses its default member initializer, otherwise it is initialized from {}; an omitted reference without an initializer causes an error. Thus, in the example, b.y is 10 and c.x is 0. These rules target C++23; the supplied site hosts an evolving draft, not frozen C++23 wording.

Examples

#include <iostream>

struct Record {
    int x;
    int y = 10;
};

int main() {
    Record a{3, 4};
    Record b{.x = 7};
    Record c{};
    std::cout << a.x << ' ' << a.y << '\n';
    std::cout << b.x << ' ' << b.y << '\n';
    std::cout << c.x << ' ' << c.y << '\n';
}

Result

3 4
7 10
0 10

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources