Namespaces
Variants
Views
Actions

value initialization

From cppreference.com
< cpp‎ | language
Revision as of 08:50, 18 September 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Provides the default initial value to a new object.

Contents

Syntax

T object {}; (1) (since C++11)
T();

T{};

(2)
(since C++11)
new T ();

new T {};

(3)
(since C++11)

Explanation

Value initialization is performed in three situations:

1) when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (since C++11)
2) when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
3) when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces.

In all cases, if the empty pair of braces {} is used, and T is a class type that has no default constructor but has a constructor taking std::initializer_list, list-initialization is performed. If T is an aggregate type, aggregate-initialization is performed.

The effects of value initialization are:

  • If T is a class type with at least one user-provided constructor of any kind, the default constructor is called.
  • If T is an non-union class type without any user-provided constructors, then the object is zero-initialized and then the implicitly-declared default constructor is called (unless it's trivial)
  • If T is an array type, each element of the array is value-initialized
  • Otherwise, the object is zero-initialized.

Notes

The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns T. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.

References cannot be value-initialized.

All standard containers (std::vector, std::list, etc) value-initialize their elements when constructed with a single size_type argument or when grown by a call to resize().

Example

#include <string>
#include <vector>
#include <iostream>
 
struct T1 {
    int mem1;
    std::string mem2;
}; // no constructors
struct T2 { 
    int mem1;
    std::string mem2;
    T2(const T2&) {} // a constructor, but no default
};
struct T3 { 
    int mem1;
    std::string mem2;
    T3() {} // user-provided default ctor
};
 
std::string s{}; // calls default ctor, the value is "" (empty string)
int main()
{
    int n{};     // non-class value-initialization, value is 0
    double f = double(); // non-class value-init, value is 0.0
    int* a = new int[10](); // array of 10 zeroes
 
    T1 t1{}; // no ctors: zero-initialized
             // t1.mem1 is zero-initialized
             // t1.mem2 is default-initialized
//    T2 t2{}; // error: has a ctor, but no default ctor
    T3 t3{}; // user-defined default ctor:
             // t3.mem1 is default-initialized (the value is indeterminate)
             // t3.mem2 is default-initialized
 
    std::vector<int> v(3); // value-initializes three ints
 
    std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n';
    std::cout << t1.mem1 << ' ' << t3.mem1 << '\n';
    delete[] a;
}

Output:

0 0 0 0 0
0 4199376

See also