I am very confused about value- & default- & zero-initialization. and especially when they kick in for the different standards C++03 and C++11 (and C++14).
I am quoting and trying to extend a really good answer Value-/Default-/Zero- Init C++98 and C++03 here to make it more general as it would help a lot of users if somebody could help fill out the needed gaps to have a good overview about what happens when?
The full insight by examples in a nutshell:
Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is a POD (plain old data), or if it's a class that contains POD members and is using a compiler-generated default constructor.
- In C++1998 there are 2 types of initialization: zero- and default-initialization
- In C++2003 a 3rd type of initialization, value-initialization was added.
- In C++2011/C++2014 only list-initialization was added and the rules for value-/default-/zero-initialization changed a bit.
Assume:
struct A { int m; };
struct B { ~B(); int m; };
struct C { C() : m(){}; ~C(); int m; };
struct D { D(){}; int m; };
struct E { E() = default; int m;}; /** only possible in c++11/14 */
struct F {F(); int m;}; F::F() = default; /** only possible in c++11/14 */
In a C++98 compiler, the following should occur:
new A
- indeterminate value (A
is POD)new A()
- zero-initializenew B
- default construct (B::m
is uninitialized,B
is non-POD)new B()
- default construct (B::m
is uninitialized)new C
- default construct (C::m
is zero-initialized,C
is non-POD)new C()
- default construct (C::m
is zero-initialized)new D
- default construct (D::m
is uninitialized,D
is non-POD)new D()
- default construct? (D::m
is uninitialized)
In a C++03 conformant compiler, things should work like so:
new A
- indeterminate value (A
is POD)new A()
- value-initializeA
, which is zero-initialization since it's a POD.new B
- default-initializes (leavesB::m
uninitialized,B
is non-POD)new B()
- value-initializesB
which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.new C
- default-initializesC
, which calls the default ctor. (C::m
is zero-initialized,C
is non-POD)new C()
- value-initializesC
, which calls the default ctor. (C::m
is zero-initialized)new D
- default construct (D::m
is uninitialized,D
is non-POD)new D()
- value-initializes D?, which calls the default ctor (D::m
is uninitialized)
Italic values and ? are uncertainties, please help to correct this :-)
In a C++11 conformant compiler, things should work like so:
??? (please help if I start here it will anyway go wrong)
In a C++14 conformant compiler, things should work like so: ??? (please help if I start here it will anyway go wrong) (Draft based on answer)
new A
- default-initializesA
, compiler gen. ctor, (leavsA::m
uninitialized) (A
is POD)new A()
- value-initializesA
, which is zero-initialization since 2. point in [dcl.init]/8new B
- default-initializesB
, compiler gen. ctor, (leavsB::m
uninitialized) (B
is non-POD)new B()
- value-initializesB
which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.new C
- default-initializesC
, which calls the default ctor. (C::m
is zero-initialized,C
is non-POD)new C()
- value-initializesC
, which calls the default ctor. (C::m
is zero-initialized)new D
- default-initializesD
(D::m
is uninitialized,D
is non-POD)new D()
- value-initializesD
, which calls the default ctor (D::m
is uninitialized)new E
- default-initializesE
, which calls the comp. gen. ctor. (E::m
is uninitialized, E is non-POD)new E()
- value-initializesE
, which zero-initializesE
since 2 point in [dcl.init]/8 )new F
- default-initializesF
, which calls the comp. gen. ctor. (F::m
is uninitialized,F
is non-POD)new F()
- value-initializesF
, which default-initializesF
since 1. point in [dcl.init]/8 (F
ctor function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. Link)
struct D { D() {}; int m; };
may be worth including in your list. – Cutcheon