gpt4 book ai didi

c++ - 值初始化是 C++98 标准的一部分吗?如果不是,为什么在 C++03 标准中添加它?

转载 作者:IT老高 更新时间:2023-10-28 23:18:45 25 4
gpt4 key购买 nike

干杯。 - Alf 在 answer 中发表了评论与 C++98 相比,值初始化可以说是 C++03 的一个新特性。我想知道他的意思。

value initialization C++98 的一部分?它是否存在于概念中但不在名称中?为什么将其添加到 C++03 标准中?

我有一份 '03 标准,但没有 '98 标准。这里是默认初始化和值初始化的定义。

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

我的猜测是 '98 有默认初始化,但没有值初始化,两者之间存在一些关键区别。老实说,我在这里无法解析标准语,而且我不明白定义之间的区别。

最佳答案

引用 the ISO/IEC 14882:1998 standard document (已从 ISO 中撤消):

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 storage for the object is zero-initialized.

在第 7 段中:

An object whose initializer is an empty set of parentheses, i.e., (), shall be default-initialized.

有关更改背后的基本原理的详细信息,请参阅 the defect report这让它发生了:

This definition is appropriate for local variables, but not for objects that are initialized as a result of executing expressions of the form T(), because the objects yielded by such expressions will be copied immediately, and should therefore have values that are assured of being copyable.
To this end, I propose adding the following new text to 8.5, paragraph 5:

To value-initialize an object of type T means:

  • if T is a class type (clause 9 [class]) 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 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 storage for the object is zero-initialized.

In addition, I propose to change ‘‘default-initialization’’ to ‘‘value-initialization’’ in 5.2.3 paragraph 2.

然后是历史解释:

Ancient history

Once upon a time, an AT&T compiler developer named Laura Eaves asked me: ‘‘What should be the value of int()?’’ My first thought was that it should be the same value as x has after saying

int x;

but I soon realized that that definition would not do. The reason is that x has an indeterminate value (assuming that it is a local variable), but we don’t mind that x is indeterminate, because we are presumably going to assign a value to x before we use it. In contrast, int() had better not have an indeterminate value, because copying such a value has an undefined effect. It would be silly to forbid a compiler from flagging int() during compilation, only to allow it to flag it during execution! […]

关于c++ - 值初始化是 C++98 标准的一部分吗?如果不是,为什么在 C++03 标准中添加它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27349679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com