Meaning of default initialization changed in C++11?

前端 未结 3 1229
栀梦
栀梦 2020-12-05 13:14

C++2003 8.5/5 says:

To default-initialize an object of type T means:

— if T is a non-POD class type (clause 9), the default const

相关标签:
3条回答
  • 2020-12-05 13:38

    According to cppreference.com (because it uses friendlier language than the standard):

    Default initialization is performed in three situations:

    3) when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.

    Value initialization is performed in three situations:

    3,7) when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses or braces (since C++11)

    Note that the C++11 part belongs with the or braces, not with the entire paragraph.

    And:

    To value-initialize an object of type T means:
    — if T is an array type, then each element is value-initialized;
    — otherwise, the object is zero-initialized

    So in C++11 default-initialization does not zero-initialize members but value-initialization does.

    0 讨论(0)
  • 2020-12-05 13:39

    Strictly speaking, the definition of default-initialize has changed from C++03 to C++11. But one has also to take into account that the situations when an object is _default-initialize_d changed:

    §8.5p9 C++03 states:

    If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

    §8.5p11 C++11 states:

    If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

    As @JamesKanze already pointed out, default-initialization is performed in C++03 when no initializer for an object of non-POD class type is specified. In C++11 an object (of arbitrary type) is default-initialized if no initializer is specified. Because of this change, the definition of default-initialize had also to be changed in order to be compatible with C++03.


    Your example has nothing to do with default-initialization. It has been always the case that an object whose initializer is an empty set of parentheses is value-initialized.

    0 讨论(0)
  • 2020-12-05 13:41

    The final effects are almost the same. In C++03, the use of default-initialize was restricted to non-POD class type, so the last point never applied. In C++11, the standard simplifies the wording by eliminating the condition with regards to where default-initialization was used, and changes the definition of default-initialization to cover all of the cases in a way to correspond what happened before.

    0 讨论(0)
提交回复
热议问题