这个自初始化有效吗? [英] Is this self initialization valid?

查看:135
本文介绍了这个自初始化有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我以前想过,但认为这不是微不足道的回答

I have this question, which i thought about earlier, but figured it's not trivial to answer

int x = x + 1;
int main() {
  return x;
}

我的问题是程序的行为是定义还是未定义在所有。如果它被定义,是 x main 中已知的值?

My question is whether the behavior of the program is defined or undefined if it's valid at all. If it's defined, is the value of x known in main?

推荐答案

我确定它是定义的,x应该有值1.§3.6.2/ 1说:具有静态存储持续时间(3.7.1)的对象在任何其他初始化之前应该被初始化(8.5)。

I'm pretty sure it's defined, and x should have the value 1. §3.6.2/1 says: "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place."

之后,我认为这是非常简单的。

After that, I think it's all pretty straightforward.

这篇关于这个自初始化有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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