C++ = default vs {}: initialization rules that change behavior

10 steps · about 2 h 35 min · Intermediate

  1. 1.4 — Variable assignment and initialization (tutorial, learncpp.com, 15 min)

    It introduces default-, value- and zero-initialization, which the rest of the list builds on.

  2. 13.8 — Struct aggregate initialization (tutorial, learncpp.com, 15 min)

    Empty braces on a plain struct do aggregate or value initialization, so you need to know what an aggregate is first.

  3. 13.9 — Default member initialization (tutorial, learncpp.com, 10 min)

    It shows how `T x;` and `T x{};` differ for aggregates, and how default member initializers remove the risk.

  4. 14.10 — Constructor member initializer lists (tutorial, learncpp.com, 15 min)

    It covers how constructors actually initialize members, before you compare an empty body with `= default`.

  5. 14.11 — Default constructors and default arguments (tutorial, learncpp.com, 15 min)

    It states the key rule: value initialization zeroes a class first when its default constructor isn't user-provided.

  6. Constructors (C++) (doc, Microsoft Learn, 20 min)

    This is a broader official reference on constructor kinds, member init lists and composite classes.

  7. Quick Q: How is “=default” different from “{}” for default constructor and destructor? (article, Standard C++ Foundation, 10 min)

    It's the classic short answer: `= default` keeps the type trivial and `{}` doesn't.

  8. Default constructors - cppreference.com (reference, cppreference.com, 15 min)

    It gives the precise rules for implicit, defaulted and deleted default constructors.

  9. Tip of the Week #146: Default vs Value Initialization (article, Abseil, 15 min)

    It's a practical Google engineering note on when members end up uninitialized, and why `= default` on first declaration matters.

  10. Initialization in C++ is Bonkers (article, Sy Brand (TartanLlama), 25 min)

    It sorts out the whole initialization zoo, including the edge cases you will meet in real code.