This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


2366. Can default initialization be constant initialization?

Section: 6.9.3.2  [basic.start.static]     Status: CD5     Submitter: Geoffrey Romer     Date: 2017-11-01

[Accepted as a DR at the July, 2019 meeting.]

According to 6.9.3.2 [basic.start.static] paragraph 2,

Constant initialization is performed if a variable or temporary object with static or thread storage duration is initialized by a constant initializer for the entity. If constant initialization is not performed, a variable with static storage duration (6.7.5.2 [basic.stc.static]) or thread storage duration (6.7.5.3 [basic.stc.thread]) is zero-initialized (9.4 [dcl.init]). Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization.

This appears to require an explicit initializer for constant initialization and would preclude examples like:

  struct S {
    int i = 1;
  };
  static constexpr S s;

where there is no initializer.

Notes from the October, 2018 teleconference:

CWG agreed that the example should be well-formed.

Proposed resolution, June, 2019:

  1. Change 6.9.3.2 [basic.start.static] paragraph 2 as follows:

  2. Constant initialization is performed if a variable or temporary object with static or thread storage duration is constant-initialized by a constant initializer (7.7 [expr.const]) for the entity. If constant initialization is not performed, a variable with static storage duration (6.7.5.2 [basic.stc.static]) or thread storage duration (6.7.5.3 [basic.stc.thread]) is zero-initialized (9.4 [dcl.init]). Together, zero-initialization and constant initialization...
  3. Change 7.7 [expr.const] paragraph 2 as follows:

  4. A constant initializer for a variable or temporary object o is an initializer for which interpreting its full-expression as a constant-expression results in a constant expression constant-initialized if

  5. Change 7.7 [expr.const] paragraph 3 as follows:

  6. A variable is usable in constant expressions after its initializing declaration is encountered if it is a constexpr variable, or it is a constant-initialized variable of reference type or of const-qualified integral or enumeration type, and its initializer is a constant initializer.
  7. Change 9.2.6 [dcl.constexpr] paragraph 5 as follows:

  8. For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (7.7 [expr.const]), or, for a constructor, a constant initializer for an evaluated subexpression of the initialization full-expression of some constant-initialized object (6.9.3.2 [basic.start.static]), the program is ill-formed, no diagnostic required. [Example:...