New Initializer
使用new时,我们通常会这么写:
1. newT
2. newT()
3. newT(value)
C++03标准中(p82), 15小节这么定义这三种new的方式
A new-expression that creates an object of type T initializes that object as follows:
1. If the new-initializer is omitted:
1.1 If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default initialized(8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.
1.2 Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;
2. If the new-initializer is of the form (), the item is value-initialized (8.5);
3. If the new-initializer is of the form (expression-list) and T is a class type, the appropriate constructor is called, using expression-list as the arguments (8.5);
4. If the new-initializer is of the form (expression-list) and T is an arithmetic, enumeration, pointer, or pointer-to-member type and expression-list comprises exactly one expression, then the object is initialized to the (possibly converted) value of the expression (8.5);
5. Otherwise the new-expression is ill-formed.
对于标准中的1,也就是对应new T这种情况。这里标准中说,如果T是一个Non-POD类类型,那么new出的对象将被default intialized.否则,如果T是POD类型,那么只分配内存,不做初始化。
对于new T(),标准中规定,对象是被value initialized。
对于new T(value),标准中规定,如果对象是个类类型,那么将会调用适当的构造函数。如果对象是数值、枚举、指针、指向成员的指针类型,那么将会用value来初始化该对象。
这涉及到c++中3中initialization方式
1. zero initialization
2. default initialization
3. value initialization
C++03的(p185) 8.5节中这么定义这3个初始化
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
— if T is a union type, the object’s first named data member89) is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.
To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
An object whose initializer is am empty set of parentheses, i.e., (), shall be value-initialzed.
所谓zero initialization,也就是将一个对象的所占用的内存全部设置为0。对于内建类型的静态变量,就属于这种初始化。
default initialization对于Non-POD的类类型,将会调用默认构造函数,对于数组类型,每个数组元素将会被默认构造。否则,如果是内建类型等non-POD类型,则会zero initialized.
value initailation是调用对象的相应构造函数,而对于内建类型等非类类型,则会被zero initialized.
Example:
通常我们在写模板时,会用以下的代码来初始化一个类型对象
template<typename T>
class foo
{
T v;
public:
foo()
: v() {} // 默认构造,适用于value initialization
};
v的值被value initialized
1. 如果v是内建类型,那么将被zero initialized
2. 如果v有用户定义的默认构造函数,那么调用该函数
3. 如果v没有用户定义的函数,那么每个非静态成员被被value-initialized。
相关知识: POD class type
标准中这么定义POD类类型:
A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public
by default (clause 11). A union is a class defined with the class-key union; its members are public by
default and it holds only one data member at a time (9.5). [Note: aggregates of class type are described in
8.5.1. ] A POD-struct is an aggregate class that hasno non-static data members of type non-POD-struct,
non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator
and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data
members of type non-POD-struct, non-POD-union (or array of such types) or reference, andhas no userdefined
copy assignment operator and no user-defined destructor. A POD class is a class that is either a
POD-struct or a POD-union.
标准中还说,对于内建的数值、指针、枚举等类型,统称为POD类型。原话是:
Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cvqualified
versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types,
POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively
called POD types.
98和0x中对POD class type的定义都依赖于aggregate class,而aggregate class的定义比较严格,不能有虚函数,不能有基类,不能有用户定义的构造函数,不能有非static的non-public成员。而0x中相对比较宽松.
reference:
C++ 03 standarad
C++ 0x standard draft