一个静态constexpr变量的自初始化,它是否形式? [英] Self-initialization of a static constexpr variable, is it well-formed?

查看:297
本文介绍了一个静态constexpr变量的自初始化,它是否形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定全局命名空间中的以下声明:

Given the following declaration in the global namespace:

constexpr int x = x;

这是否格式良好?

草稿C ++ 14标准版面 3.6.2 [basic.start.init] 说:

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


在任何其他初始化发生之前,具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的变量应该是零初始化(8.5)
。 [...]

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

看起来好像定义的例子是 x 在常数初始化期间用其自己的值初始化,由于零初始化,它将 0

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.

真的是这样吗? clang 接受此代码,而 gcc产生诊断

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;
                  ^


推荐答案

缺陷报告2026:Zero-initialization and constexpr 其中询问:


根据3.6.2 [basic.start.init]第2段,

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


具有静态存储持续时间的变量(3.7.1 [basic.stc.static])或
线程存储持续时间(3.7.2 [basic.stc.thread ])在任何其他初始化
发生之前应为
零初始化(8.5 [dcl.init])。

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同意在这些情况下应将常量初始化视为发生,而不是零初始化,使声明不成形。

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:


在任何其他初始化发生之前,具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的变量应该被初始化为(8.5)
。 [...]

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

并添加以下措辞:


如果不执行常量初始化,则具有静态存储持续时间(3.7.1 [basic.stc.static])或线程存储持续时间(3.7.2 [basic.stc.thread])的变量为零,初始化(8.5 [dcl.init])。 [...]

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

这是一个很大的变化,它重命名 [basic.start.init]到 [basic.start.static] 并创建了一个新的 [basic.start.dynamic] 部分并修改 [stmt.dcl]

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]

这篇关于一个静态constexpr变量的自初始化,它是否形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆