Self-initialization of a static constexpr variable, is it well-formed?

后端 未结 1 1812
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 09:15

Given the following declaration in the global namespace:

constexpr int x = x;

Is this well-formed?

The draft C++14 standard section

相关标签:
1条回答
  • 2020-11-29 09:54

    This was clarified and made ill-formed by defect report 2026: Zero-initialization and constexpr which asks:

    According to 3.6.2 [basic.start.init] paragraph 2,

    Variables with static storage duration (3.7.1 [basic.stc.static]) or thread storage duration (3.7.2 [basic.stc.thread]) shall be zero-initialized (8.5 [dcl.init]) before any other initialization takes place.

    Does this apply to constant initialization as well? For example, should the following be well-formed, relying on the presumed zero-initialization preceding the constant initialization?

    constexpr int i = i;
    struct s {
      constexpr s() : v(v) { }
      int v;
    };
    constexpr s s1;
    

    The note before the proposed resolution says:

    CWG agreed that constant initialization should be considered as happening instead of zero initialization in these cases, making the declarations ill-formed.

    and the proposed resolution clarifies and amongst many changes, removes the following wording:

    Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. [...]

    and adds the following wording:

    If constant initialization is not performed, a variable with static storage duration (3.7.1 [basic.stc.static]) or thread storage duration (3.7.2 [basic.stc.thread]) is zero-initialized (8.5 [dcl.init]). [...]

    It is a large change, it renames [basic.start.init] to [basic.start.static] and created a new section [basic.start.dynamic] and modifies [stmt.dcl]

    0 讨论(0)
提交回复
热议问题