为什么真空初始化的概念是必要的?

Why is the concept of vacuous initialization necessary?

vacuous initialization的概念在[basic.life/1]被引入并使用,在C++标准的其他任何地方似乎都没有使用:

The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly multi-dimensional) array thereof, that class type has a trivial default constructor. The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) ([dcl.init]),

由于vacuous初始化是默认初始化的一个子集,所以对于class和数组类型是初始化不初始化 对于标量类型(参见 [dcl.init.general/7]),因此这两种情况都包含在短语“初始化(如果有)”中。

那么为什么空初始化的概念是必要的?

引用似乎等同于这个修改后的版本:

The lifetime of an object or reference is a runtime property of the object or reference. The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete ([dcl.init]),

OP 引用的当前标准草案已通过 CWG issue 2256 (2256. Lifetime of trivially-destructible objects) and P1787R6 达到其状态(P1787R6:声明及其查找位置),之后,如果any,术语(而不是“概念”)可以用来统指“初始化”不足够的情况。

最起码是指[stmt.dcl]/3:

Upon each transfer of control (including sequential execution of statements) within a function from point P to point Q, all variables that are active at P and not at Q are destroyed in the reverse order of their construction. Then, all variables that are active at Q but not at P are initialized in declaration order; unless all such variables have vacuous initialization ([basic.life]), the transfer of control shall not be a jump. [...]

这里简单的“初始化”自然是不够的,因为变量可以有非空初始化,这样在点 P 到点 Q 之间的跳转将违反 [[=28 的限制=]]/3.

如果我们按照提议修改[basic.life/1],该术语将不再定义,例如[stmt.dcl]/3 需要显式写出允许跳转(在 P->Q 限制下)的完整初始化上下文。