skip to content

Why did adding ConnectionConfig() = default; stop ConnectionConfig{"db", 6432} from compiling when a C++ project moved to C++20?

level: middleimportance: should knowfreq 42%

answer

  1. brace-init of members needs an aggregate
  2. user-declared versus user-provided
  3. C++20 tightened the definition
  4. remove the defaulted constructor

basics

~20 s

Since C++20 a class with any user-declared constructor, even one written = default, is not an aggregate. ConnectionConfig{"db", 6432} relied on aggregate initialisation, so it now looks for a two-argument constructor that does not exist.

solid answer

~50 s

Brace-initialising members in order, `ConnectionConfig{"db", 6432}`, is **aggregate initialisation**, available only for aggregates. Up to C++17 an aggregate could not have *user-provided* constructors, and `= default` on the first declaration is not user-provided, so the struct still qualified. C++20 changed the rule to *no user-declared or inherited constructors*; the standard's compatibility annex lists the change, with the rationale of removing error-prone aggregate initialisation that bypassed a class's declared constructors. Under `-std=c++20`, Clang reports `no matching constructor for initialization of 'ConnectionConfig'`. The fix is usually to delete the defaulted constructor: default member initializers already give a default state, and they have kept a class an aggregate since C++14. Remaining aggregate rules: no private or protected direct non-static data members or bases, and no virtual functions or virtual bases. In return, C++20 aggregates gain designated initializers such as `{.host = "replica", .timeoutMs = 250}`.

code

cpp · 15 lines
cpp
#include <cstdio>
#include <string>

struct ConnectionConfig {          // an aggregate: no user-declared constructors
    std::string host = "localhost";
    int port = 5432;
    int timeoutMs = 1000;
};

int main() {
    ConnectionConfig a{"db.internal"};                         // remaining members use defaults
    ConnectionConfig b{.host = "replica", .timeoutMs = 250};   // designated initializers
    std::printf("%s:%d/%d %s:%d/%d\n", a.host.c_str(), a.port, a.timeoutMs,
                b.host.c_str(), b.port, b.timeoutMs);
}

go deeper

for a junior

Recall that brace-initialising members in order works only for aggregates, and that declaring a constructor removes that ability in C++20.

for a middle

Explain user-declared versus user-provided, the C++20 rule change, and the compiler error it produces when a project upgrades its standard.

for a senior

Plan a standard upgrade by finding structs with defaulted or deleted constructors that are still brace-initialised, and choose aggregate or constructor-owning designs deliberately.

for a principal

Set a convention: passive records stay aggregates with default member initializers and designated initializers, types with invariants own their constructors and private data.

## Aggregates and aggregate initialisation An **aggregate** is an array, or a class that the language lets you initialise member by member with braces, with no constructor involved. `ConnectionConfig{"db.internal", 6432}` copies `"db.internal"` into the first member and `6432` into the second; any remaining members use their default member initializers or are value-initialised. The C++23 standard defines an aggregate as an array or a class with: - no **user-declared or inherited constructors**, - no **private or protected direct non-static data members**, - no **private or protected direct base classes**, and - no **virtual functions** or **virtual base classes**. Default member initializers are allowed: the compatibility annex records that classes with default member initializers can be aggregates from C++14 onwards. ## What changed in C++20 Before C++20 the first condition was about **user-provided** constructors. A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration, so `ConnectionConfig() = default;` was user-declared but not user-provided, and the type stayed an aggregate. C++20 replaced the condition with "no user-declared or inherited constructors". The standard's compatibility annex describes it: > A class that has user-declared constructors is never an aggregate. Its stated rationale is to remove potentially error-prone aggregate initialisation which may apply notwithstanding the declared constructors of a class. The annex gives cases that were well-formed in C++17 and are not now: 1. `struct A { A() = delete; };` and `A a{};` used to compile, because aggregate initialisation ignored the deleted constructor. 2. `struct B { B() = default; int i = 0; };` and `B b = {1};` used to aggregate-initialise. 3. A struct with a defaulted move constructor stopped being an aggregate as well. The first case shows why the rule changed: a class could delete its default constructor to forbid creation, and braces walked straight past it. ## Seeing it in the compiler The same file, compiled twice: | Standard | `std::is_aggregate_v<ConnectionConfig>` | `ConnectionConfig cfg{"db.internal", 6432};` | |---|---|---| | `-std=c++17` | `1` | aggregate initialisation, prints `db.internal:6432` | | `-std=c++20` | `0` | `error: no matching constructor for initialization of 'ConnectionConfig'` | The error mentions constructors because, without aggregate status, braces mean "call a constructor", and the class has only a default one plus the implicit copy and move constructors. ## Fixing the type Choose one of two shapes and be consistent: - **Keep it an aggregate.** Delete `ConnectionConfig() = default;`. Default member initializers already give the type its default state, and `ConnectionConfig cfg;` and `ConnectionConfig cfg{};` still work. This is the usual fix for a passive configuration record. - **Make it a class with constructors.** If the type must validate its fields, write the constructors you need, make the data private, and stop relying on member-wise braces. Mixing the two, with public data members and a defaulted constructor, buys nothing since C++20 and costs aggregate initialisation. ## What C++20 aggregates gained The same revision added **designated initializers**: `ConnectionConfig b{.host = "replica", .timeoutMs = 250};`. Designators must appear in declaration order, and members not named take their default member initializers, so the code example prints `replica:5432/250`. Designated initialisation works only on aggregates, which is one more reason to keep configuration records free of constructors. ## Upgrading a codebase to C++20 The change surfaces as compile errors when a project raises its standard, usually far from the struct that caused them: 1. Build with `-std=c++20` and look for `no matching constructor for initialization of` errors on brace-initialised records. 2. For each type involved, look for **defaulted or deleted constructors** that were added as documentation or to silence a warning. 3. Decide per type: delete the defaulted constructor to keep an aggregate, or give the type real constructors. 4. Add `static_assert(std::is_aggregate_v<ConnectionConfig>);` next to records whose callers rely on brace or designated initialisation, so the next accidental constructor fails at the definition instead of at every caller. Because the meaning can change silently in rare cases, such as the annex's example where braces stop meaning aggregate initialisation and start calling a copy constructor, review the diff even where the code still compiles. ## How to answer Name the rule change precisely: user-provided became user-declared in C++20. Give the motivating hole (a deleted constructor bypassed by braces), the compiler error, and the fix (remove the defaulted constructor and rely on default member initializers).

  • In C++20, does a struct with default member initializers still count as an aggregate?
    Yes. Default member initializers have been compatible with aggregates since C++14, and C++20 did not change that. Members not covered by the braces take their default member initializers, so `ConnectionConfig{"db"}` leaves `port` at 5432. Only constructors, private or protected direct data or bases, and virtual functions or bases disqualify a class.
  • In C++, why was A() = delete; with A a{}; a problem before C++20?
    Up to C++17, a deleted constructor was not user-provided, so `A` remained an aggregate and `A a{};` aggregate-initialised it without calling any constructor, defeating the deletion meant to forbid creation. C++20's rule, no user-declared constructors, makes `A` a non-aggregate, so the braces must call a constructor and hit the deleted one.

  • = default never changes anything about how a class can be initialised.
  • A struct with default member initializers cannot be brace-initialised member by member.
  • Only user-provided constructors disqualify an aggregate in C++20.
  • Designated initializers work with any class that has public members.
  • The class keyword stops a type from being an aggregate.

Assumes C++20 or later; up to C++17 a class with only defaulted or deleted constructors could still be an aggregate.