Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

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

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;
                  ^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...