Is a declaration valid inside an if block with no actual block?

前端 未结 2 1850
醉话见心
醉话见心 2021-02-20 08:44

Is the following code valid? If so, what is the scope of x?

int main()
{
   if (true) int x = 42;
}

My intuition says that there i

2条回答
  •  星月不相逢
    2021-02-20 09:21

    My Visual studio says that time of life of your variable x is pretty small - just while we are inside operator if, so x vill be destroyed when we are out of if condition, and there is absolutely no meaning to declare variables like this.

提交回复
热议问题