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

下列说法正确的是?

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

它们基于 [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

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

https://stackoverflow.com/questions/69206296/

相关文章:

angular - TS2322 : Type 'HTMLDivElement' is not as

python - 检查文件夹,如果不存在则创建它

c++ - 从 std `vector` 库创建的 `` 和从 : `STL vec

ios - 是否有修改器可以更改 SwiftUI Picker 的标签颜色?

python - 正则表达式:在前瞻断言的最后一场比赛之后直到前瞻断言的第一场比赛之后查找文本

python - 如何检查字符串中的特定元素在python中是否相等?

python - 无法让 VS Code 将参数从 launch.json 传递给 Python

android - 无法查询属性 'namespace' 的值

regex - 如何在 perl regex 替换命令中使用 unicode 字符?

javascript - Vue 3 defineEmits 打破了 defineProps 类型