C++ Reference ยท Types and Language Concepts
Initialization in C++
Concept guide to C++ initialization: default, value, direct, copy and list initialization, zero-initialization of statics, aggregates and the most vexing parse.
C++ has several ways to give an object its first value, and they differ in what happens when you leave the value out. int x; inside a function leaves x indeterminate, int x{}; sets it to zero, and std::string s; calls a constructor. Knowing which kind of initialization you are getting prevents a whole class of bugs.
The kinds are named by their syntax and by what they do: default, value, direct, copy, list, aggregate and zero initialization. This page explains each one and how to pick between them. A good rule for new code is to initialize every variable where you declare it, and to prefer braces when you want the "no surprises" version.
int a; // default-initialized: indeterminate for a local int int b = 5; // copy initialization int c(5); // direct initialization int d{5}; // direct-list initialization int e{}; // value initialization: 0
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T x; // default initialization T x{}; T x = T(); // value initialization T x(args); // direct initialization T x = value; // copy initialization T x{args}; // direct list initialization T x = {args}; // copy list initialization T x(args...) // C++20: parenthesized aggregate initialization
| Kind | What it does |
|---|---|
| Default | Class types: call the default constructor. Built-in types and arrays of them: leave indeterminate (unless static/thread storage). |
| Value | T() or T{}: zero-initialize, then call the default constructor if it is not user-provided. A class with a user-provided default constructor just runs it. |
| Direct | Calls the constructor chosen by overload resolution; explicit constructors are allowed. |
| Copy | T x = v; converts v to T through implicit conversions; explicit constructors are not considered. |
| List | Brace syntax; prefers std::initializer_list constructors, forbids narrowing. See Brace initialization. |
| Zero | Sets all bits to zero (null pointers, 0, 0.0). Applied to statics before any other initialization. |
| Aggregate | Initializes each member from the brace list in order. See Designated initializers. |
Errors / undefined behavior: reading an indeterminate value (a local int that was default-initialized) is undefined behavior, except for narrow unsigned char and std::byte in specific cases.
Usage notes
- Initialize at declaration.
int count = 0;costs nothing and removes the indeterminate-value trap. Declare the variable where you can give it a meaningful value. - Static storage is zero first. Namespace-scope and
staticvariables are zero-initialized, then dynamically initialized if they have a non-constant initializer. Across translation units the order of dynamic initialization is unspecified (the "static initialization order fiasco"). - Default member initializers (
int a = 1;) apply in every constructor that does not mention that member. They keep multi-constructor classes consistent. - Member initializer lists run in declaration order, not in the order you write them. Initializing a member from a later one is a bug that
-Wreorderwarns about. - The most vexing parse.
T x();declares a function returningT. WriteT x;,T x{};orT x = T();for an object. explicitmatters only for copy initialization.std::vector<int> v = 5;fails,std::vector<int> v(5);works.autowith braces changed in C++17:auto x{1};deducesint,auto y = {1};deducesstd::initializer_list<int>.- Parenthesized aggregate initialization (C++20) allows
Plain p(1, 2);. It does not forbid narrowing, and it letsstd::make_uniqueandemplace_backbuild aggregates. - Dangling in constructors. Initializing a reference member from a temporary leaves it dangling after the constructor ends.