Initializer of C++ objects


New Initializer
newを使用する場合、私たちは通常このように書きます:1.newT      2. newT()      3. newT(value)
C++03規格では(p 82)、15節でこの3つの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の(p 185)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とは、1つのオブジェクトの消費メモリをすべて0に設定することです.組み込みタイプの静的変数は、この初期化に属します.
default initializationはNon-PODのクラスタイプに対してデフォルトコンストラクション関数を呼び出し、配列タイプに対して各配列要素がデフォルトコンストラクションされます.そうでなければ、組み込みタイプなどのnon-PODタイプであれば、zero initializedとなります.
value initailationは呼び出しオブジェクトの対応する構造関数であり、組み込みタイプなどの非クラスタイプについてはzero initializedとなる.
 
   Example:
 
 int* p = new int;//intの値は、new rule 1 int*p 2=new int();//*に不確定になります.p 2=0はnew rule 2に合致し、value initalized int*p 3=new int(3);//*p 3=3はnew rule 4に適合し、このint*p 4=new intを3で初期化する[4];//new rule 1に適合し、すべての配列要素の値が不確定//new rule 2に適合し、ここではnew initializerが追加され、各配列要素がvalue initiailized//p 5[i]=0 int*p 5=new int[4];//non-POD class type class foo { int i; };//default initializedは、デフォルトのコンストラクション関数を呼び出します.ユーザー定義がないため、コンパイラが自動的に生成するctorを呼び出します.つまり、何もしません//p->iの値はfoo*p=new fooです.//new rule 1.1//value initialziedに準拠しますが、このタイプにはユーザー定義のctorがないため、各メンバーは自動的にvalue initialzed//ここiは組み込みタイプなので、0 foo*p 2=new foo()//に初期化されます.new rule 2//各配列要素はdefault initialized、すなわちデフォルト構造関数を呼び出す、すなわちfoo*p 3=new foo[4];//new rule 2に準拠すると、各配列要素はvalue initialized、すなわちデフォルトのコンストラクション関数によって呼び出され、ユーザ定義//がないため、各メンバーはvalue initialzied、ここでp 3[i]->i=0 foo*p 3=new foo[4]();
 
 
通常、テンプレートを書くときに、次のコードを使用してタイプオブジェクトを初期化します.
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と0 xのPOD class typeの定義はaggregate classに依存しているが、aggregate classの定義は比較的厳格で、虚関数、ベースクラス、ユーザー定義の構造関数、static以外のnon-publicメンバーは存在しない.0 xでは比較的緩やかである.
 
 
reference:
      C++ 03 standarad
      C++ 0x standard draft