I am very confused about initializing values ββand defaults and zeros. and especially when they kick for different standards C ++ 03 and C ++ 11 (and C ++ 14).
I am quoting and trying to extend a really good Value- / Default- / Zero-Init answer C ++ 98 and C ++ 03 to make this more general, as it would help many users if someone could help fill in the required spaces to have a good overview of what happens when?
A complete description of the examples in a nutshell:
Sometimes the memory returned by the new operator will be initialized, and sometimes it does not depend on whether the type you are new to is POD (plain old data) , or if it is a class containing POD elements and uses the default constructor created by the compiler.
- In C ++ 1998, there are 2 types of initialization: zero and default
- In C ++ 2003, a third type of initialization was added, value initialization.
- In C ++ 2011 / C ++ 2014, only list initialization was added, and the rules for initializing value / default // zero have 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;} struct F {F(); int m;} F::F() = default;
In the C ++ 98 compiler, the following should happen ::
- new value A - undefined value (A - POD)
- new A () - zero-initialize
- new B - default construction (B :: m is not initialized, B is not a POD)
- new B () - default construct (B :: m not initialized)
- new default C construction (C :: m is initialized to zero, C is not a POD)
- new C () - default construct (C :: m is initialized to zero)
- new D is the default constructor (D :: m is not initialized, D is not a POD)
- new D () - default construct? (D :: m is not initialized)
In a compiler compatible with C ++ 03, everything should work like this:
- new value A - undefined value (A - POD)
- new A () - initialize the value of A, which is zero initialization, since it is a POD.
- new B - initializes by default (leaves B :: m uninitialized, B is not a POD)
- new B () - the value initializes B, which zero initializes all fields, since by default ctor is generated by the compiler, unlike the user-defined one.
- new C - default - initializes C, which calls the default value ctor. (C :: m is initialized to zero, C is not a POD)
- new C () - the value initializes C, which calls the default value ctor. (C :: m is initialized to zero)
- new D is the default constructor (D :: m is not initialized, D is not a POD)
- new D () - the value initializes D?, which calls the default value ctor (D :: m is not initialized)
Italic meanings and? are uncertainties, please help fix this :-)
In a compiler compatible with C ++ 11, everything should work like this:
??? (please help if I start here, it will still go wrong)
In a compiler compatible with C ++ 14, everything should work like this: ??? (please help if I start here, it will still go wrong) (The project is based on the answer)
- new A - default - initializes A, the gen compiler. ctor, (leavs A :: m uninitialized) (A - POD)
new A () - the value initializes A, which is zero initialization, since 2. point in [dcl.init] / 8
new B - by default initializes B, the gen compiler. ctor, (leavs A :: m uninitialized) (B is not a POD)
- new B () - the value initializes B, which zero initializes all fields, since by default ctor is generated by the compiler, unlike the user-defined one.
- new C - default - initializes C, which calls the default value ctor. (C :: m is initialized to zero, C is not a POD)
- new C () - the value initializes C, which calls the default value ctor. (C :: m is initialized to zero)
- new D - default initialization D (D :: m is not initialized, D is not a POD)
- new D () - the value initializes D, which calls the default value ctor (D :: m is not initialized)
- new E - default - initializes E, which calls comp. generations. te (D :: m is not initialized, D is not a POD)
- new E () - the value initializes E, which zero initializes E, since 2 points in [dcl.init] / 8)
- new F - default - initializes F, which calls comp. generations. te (D :: m is not initialized, D is not a POD)
- new F () - the value initializes F, which by default initializes F with 1. point in [dcl.init] / 8 (a function is provided by the user if it is declared by the user and is clearly not defaulted or deleted by its first declaration. Link )
c ++ c ++ 11 c ++ 03 c ++ 14 c ++ 98
Gabriel Apr 21 '15 at 7:44 2015-04-21 07:44
source share