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

对着背影说爱祢 提交于 2019-11-26 12:29:24

问题


Given the following declaration in the global namespace:

constexpr int x = x;

Is this well-formed?

The draft C++14 standard section 3.6.2 [basic.start.init] says:

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. [...]

What seems to make the example well defined is that x is initialized with its own value during constant initialization which will be 0 due to zero initialization.

Is this really the case? clang accepts this code while gcc produces a diagnostic:

error: the value of \'x\' is not usable in a constant expression
constexpr int x = x;
                  ^

回答1:


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]



来源:https://stackoverflow.com/questions/34276373/self-initialization-of-a-static-constexpr-variable-is-it-well-formed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!