问题描述
我一直认为创建一个新对象总是会在对象上调用默认构造函数,无论该构造函数是显式的还是由编译器自动生成的,都没有任何区别。根据,这在C ++ 98和C ++ 03之间进行了微妙的更改,现在可以这样工作:
I had always thought that creating a new object would always call the default constructor on an object, and whether the constructor was explicit or automatically generated by the compiler made no difference. According to this highly regarded answer to a different question, this changed in a subtle way between C++98 and C++03 and now works like so:
struct B { ~B(); int m; }; // non-POD, compiler generated default ctor
new B; // default-initializes (leaves B::m uninitialized)
new B(); // value-initializes B which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.
谁能告诉我:
- 为什么要更改标准,即,这样做有什么好处,或者现在有什么可能是以前没有的呢?
- default-initialize(默认初始化)这些术语到底做了什么?和值初始化代表什么?
- 标准的相关部分是什么?
推荐答案
我不知道更改的基本原理(或以前的标准),但关于更改的原理,基本上 default-initialization 要么调用用户定义的构造函数或不执行任何操作(在此不费吹灰之力:这是递归地应用于每个子对象,这意味着将初始化具有默认构造函数的子对象,而没有用户定义的构造函数的子对象将保持未初始化状态。)
I do not know what the rationales around the change (or how the standard was before), but on how it is, basically default-initialization is either calling a user defined constructor or doing nothing (lots of hand-waving here: this is recursively applied to each subobject, which means that the subobjects with a default constructor will be initialized, the subobjects with no user defined constructors will be left uninitialized).
这属于只为您想要的语言付费理念,并且在与C兼容的所有类型中与C兼容。另一方面,您可以请求 value-initialization ,这等效于为具有或初始化为的对象调用默认构造函数。 0
转换为其余子对象的适当类型。
This falls within the only pay for what you want philosophy of the language and is compatible with C in all the types that are C compatible. On the other hand, you can request value-initialization, and that is the equivalent to calling the default constructor for objects that have it or initializing to 0
converted to the appropriate type for the rest of the subobjects.
这在第8.5节初始化器中进行了介绍,并且浏览起来并不容易。 zero-initialize , default-initialize 和 value-initialize 的定义是第5段:
This is described in §8.5 Initializers, and it is not trivial to navigate through. The definitions for zero-initialize, default-initialize and value-initialize are the 5th paragraph:
-如果T是标量类型(3.9),则该对象的值设置为0 (零)转换为T;
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
-如果T是非联合类类型,则将每个非静态数据成员和每个基类子对象初始化为零;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
-如果T是联合类型,则对象的第一个命名数据成员89)被零初始化;
— if T is a union type, the object’s first named data member89) is zero-initialized;
-如果T是数组类型,则每个元素都被零初始化;
— if T is an array type, each element is zero-initialized;
-如果T是引用类型,则不执行任何初始化。
— if T is a reference type, no initialization is performed.
要默认初始化类型T的对象意味着:
To default-initialize an object of type T means:
-如果T是非POD类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化格式不正确);
— 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);
-如果T是数组类型,则每个元素都将默认初始化;
— if T is an array type, each element is default-initialized;
-其他
要对值类型为T的对象进行值初始化,则意味着:
To value-initialize an object of type T means:
-如果T是类类型(第9条)中带有用户声明的构造函数(12.1),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是不正确的);
— 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);
如果T是没有用户声明的构造函数的非联合类类型,则T的每个非静态数据成员和基类组件都将值初始化;
— 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;
-如果T为数组类型,则每个元素都将被值初始化;
— if T is an array type, then each element is value-initialized;
-否则,对象将被初始化为零
— otherwise, the object is zero-initialized
一个需要默认值的程序-引用类型的实体的初始化或值初始化不正确。如果T是cv限定类型,则T的cv非限定版本用于零初始化,默认初始化和值初始化的这些定义。
A program that calls for default-initialization or value-initialization of an entity of reference type is illformed. If T is a cv-qualified type, the cv-unqualified version of T is used for these definitions of zeroinitialization, default-initialization, and value-initialization.
这篇关于C ++ 03中的default-initialize和value-initialize之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!