未初始化的引用是否为零初始化和未初始化的标量默认初始化?
Are uninitialized references zero-initialized and uninitialized scalars default-initialized?
下列说法正确吗?
- 未初始化的引用被视为零初始化。
- 未初始化的标量被认为是默认初始化的。
- 任何其他未初始化的实体都不会被视为零初始化或默认初始化。
它们基于 [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.
(同题多问:我答一个)
Is the following statement correct?
- An uninitialized reference is considered zero-initialized.
没有。没有未初始化的引用这样的东西,因为它需要引用变量或对象,而不是(引用类型)参数或(引用类型)return 类型,在这些类型中,上下文初始化不需要申请;根据 [dcl.init.ref]/1 and /3:
/1 A variable whose declared type is “reference to T” ([dcl.ref])
shall be initialized.
/3 The initializer can be omitted for a reference only in a parameter
declaration ([dcl.fct]), in the declaration of a function return type,
in the declaration of a class member within its class definition
([class.mem]), and where the extern specifier is explicitly used.
关于引用[dcl.init.general]/6:
To zero-initialize an object or reference of type T means:
- [...]
- [...] if T is a reference type, no initialization is performed.
它指的是标准的其他部分管理零初始化适用的情况(以及它的效果是什么对于不同的实体);对于引用的初始化,特别考虑静态初始化,按照 [basic.start.static]/2:
[...] If constant initialization is not performed, a variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is zero-initialized ([dcl.init]). Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. All static initialization strongly happens before ([intro.races]) any dynamic initialization.
这样引用类型变量的静态初始化(非常量初始化)将只属于动态初始化,因为静态初始化第一步零初始化的效果是没有初始化。
An uninitialized reference is considered zero-initialized.
可能是未初始化的引用已被零初始化。也就是说,零初始化的未初始化引用和未零初始化的未初始化引用之间没有明显的区别。
An uninitialized scalar is considered default-initialized.
可能是未初始化的标量已被默认初始化。也就是说,默认初始化的未初始化标量和未默认初始化的未初始化标量之间没有明显区别。
Any other uninitialized entity is not considered zero-initialized nor default-initialized.
一个实体被零初始化后被认为是零初始化,并且它被默认初始化后被认为是默认初始化。
Are the following statements correct?
不对,你是在颠倒他们的逻辑关系。
“零初始化”和“默认初始化”的定义指定了如果标准中的其他内容表示“对象是零初始化”的含义。当标准这么说时,您使用零初始化的定义来查看它的含义。作为参考,这意味着没有完成初始化。因此零初始化引用未初始化(因此格式错误)。
但这并不意味着相反。未初始化的引用不是零初始化的。这是一个谬误:https://en.wikipedia.org/wiki/Affirming_the_consequent
参考被零初始化当标准说执行零初始化时。
下列说法正确吗?
- 未初始化的引用被视为零初始化。
- 未初始化的标量被认为是默认初始化的。
- 任何其他未初始化的实体都不会被视为零初始化或默认初始化。
它们基于 [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 literal0
(zero) toT
;- 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.
(同题多问:我答一个)
Is the following statement correct?
- An uninitialized reference is considered zero-initialized.
没有。没有未初始化的引用这样的东西,因为它需要引用变量或对象,而不是(引用类型)参数或(引用类型)return 类型,在这些类型中,上下文初始化不需要申请;根据 [dcl.init.ref]/1 and /3:
/1 A variable whose declared type is “reference to T” ([dcl.ref]) shall be initialized.
/3 The initializer can be omitted for a reference only in a parameter declaration ([dcl.fct]), in the declaration of a function return type, in the declaration of a class member within its class definition ([class.mem]), and where the extern specifier is explicitly used.
关于引用[dcl.init.general]/6:
To zero-initialize an object or reference of type T means:
- [...]
- [...] if T is a reference type, no initialization is performed.
它指的是标准的其他部分管理零初始化适用的情况(以及它的效果是什么对于不同的实体);对于引用的初始化,特别考虑静态初始化,按照 [basic.start.static]/2:
[...] If constant initialization is not performed, a variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is zero-initialized ([dcl.init]). Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. All static initialization strongly happens before ([intro.races]) any dynamic initialization.
这样引用类型变量的静态初始化(非常量初始化)将只属于动态初始化,因为静态初始化第一步零初始化的效果是没有初始化。
An uninitialized reference is considered zero-initialized.
可能是未初始化的引用已被零初始化。也就是说,零初始化的未初始化引用和未零初始化的未初始化引用之间没有明显的区别。
An uninitialized scalar is considered default-initialized.
可能是未初始化的标量已被默认初始化。也就是说,默认初始化的未初始化标量和未默认初始化的未初始化标量之间没有明显区别。
Any other uninitialized entity is not considered zero-initialized nor default-initialized.
一个实体被零初始化后被认为是零初始化,并且它被默认初始化后被认为是默认初始化。
Are the following statements correct?
不对,你是在颠倒他们的逻辑关系。
“零初始化”和“默认初始化”的定义指定了如果标准中的其他内容表示“对象是零初始化”的含义。当标准这么说时,您使用零初始化的定义来查看它的含义。作为参考,这意味着没有完成初始化。因此零初始化引用未初始化(因此格式错误)。
但这并不意味着相反。未初始化的引用不是零初始化的。这是一个谬误:https://en.wikipedia.org/wiki/Affirming_the_consequent
参考被零初始化当标准说执行零初始化时。