gpt4 book ai didi

c++ - 未初始化的引用是否为零初始化和未初始化的标量默认初始化?

转载 作者:行者123 更新时间:2023-12-05 08:17:59 29 4
gpt4 key购买 nike

下列说法正确的是?

  • 未初始化的引用被视为零初始化。
  • 未初始化的标量被视为默认初始化。
  • 任何其他未初始化的实体均不被视为零初始化或默认初始化。

它们基于 [dcl.init.general/6] (大胆强调我的):

To zero-initialize an object or reference of type T means:

  • if T is a scalar type, the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;
  • if T is a (possibly cv-qualified) non-union class type, its padding bits are initialized to zero bits and each non-static data member, each non-virtual base class subobject, and, if the object is not a base class subobject, each virtual base class subobject is zero-initialized;
  • if T is a (possibly cv-qualified) union type, its padding bits are initialized to zero bits and the object's first non-static named data member is zero-initialized;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

[dcl.init.general/7] (大胆强调我的):

To default-initialize an object of type T means:

  • If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated ([over.match.ctor]), and the best one for the initializer () is chosen through overload resolution ([over.match]). The constructor thus selected is called, with an empty argument list, to initialize the object.
  • If T is an array type, each element is default-initialized.
  • Otherwise, no initialization is performed.

最佳答案

Are the following statements correct?

不,你在颠倒他们头脑中的逻辑联系。

“零初始化”和“默认初始化”的定义指定了如果标准中的其他内容表示“对象是零初始化”的含义。当标准这么说时,您使用零初始化的定义来查看它的含义。作为引用,这意味着没有完成初始化。因此零初始化引用未初始化(因此格式错误)。

但这并不意味着相反。未初始化的引用不是零初始化的。这是一个谬论:https://en.wikipedia.org/wiki/Affirming_the_consequent

当标准规定执行零初始化时,引用是零初始化的

关于c++ - 未初始化的引用是否为零初始化和未初始化的标量默认初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69206296/

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