Sets the initial value of an object to zero.
[edit] SyntaxNote that this is not the syntax for zero-initialization, which does not have a dedicated syntax in the language. These are examples of other types of initializations, which might perform zero-initialization.
static
T object ;
(1) T ()
;
T t =
{}
;
T {}
;
(since C++11)
[
n ]
=
"
short-sequence ";
(3) [edit] Explanation
Zero-initialization is performed in the following situations:
2)As part of
value-initializationsequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of
aggregatesfor which no initializers are provided.
The effects of zero-initialization are:
T
is a scalar type, the object is initialized to the value obtained by explicitly converting the integer literal â0â (zero) to T
.T
is a non-union class type:T
is a union type:T
is array type, each element is zero-initialized.T
is reference type, nothing is done.As described in non-local initialization, static and thread-local(since C++11) variables that aren't constant-initialized are zero-initialized before any other initialization takes place. If the definition of a non-class non-local variable has no initializer, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.
A zero-initialized pointer is the null pointer value of its type, even if the value of the null pointer is not integral zero.
[edit] Example#include <iostream> #include <string> struct A { int a, b, c; }; double f[3]; // zero-initialized to three 0.0's int* p; // zero-initialized to null pointer value // (even if the value is not integral 0) std::string s; // zero-initialized to indeterminate value, then // default-initialized to "" by the std::string default constructor int main(int argc, char*[]) { delete p; // safe to delete a null pointer static int n = argc; // zero-initialized to 0 then copy-initialized to argc std::cout << "n = " << n << '\n'; A a = A(); // the effect is same as: A a{}; or A a = {}; std::cout << "a = {" << a.a << ' ' << a.b << ' ' << a.c << "}\n"; }
Possible output:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior CWG 277 C++98 pointers might be initialized with a non-constantRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4