Switch-Case:带有初始化的声明&声明然后赋值

发布于 2024-09-24 06:09:32 字数 327 浏览 2 评论 0原文

在 switch-case 语句中,带初始化的声明无效,但允许声明然后赋值。如下面的代码片段所示。

从编译器方面来看,这两种类型的初始化有什么区别?为什么第一种类型的初始化无效而第二种类型的初始化有效。

switch(val)  
{  
case 0:  
  int newVal = 42;  //Invalid
  break;
case 1:  
  int newVal2;      //Valid
  newVal2 = 42;  
  break;
case 2:
  break;
}

In the switch-case statements declaration-with-initialization is invalid but declaration-and-then-assignment is allowed. As shown in the following code snippet.

What is difference between these two type of initializations from the compiler side? And why is the first type of initialization invalid and second type a valid one.

switch(val)  
{  
case 0:  
  int newVal = 42;  //Invalid
  break;
case 1:  
  int newVal2;      //Valid
  newVal2 = 42;  
  break;
case 2:
  break;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友谊不毕业 2024-10-01 06:09:32

实际上,规则是您不能跳入经过具有初始化的声明(或经过非 POD 类型变量的声明)的块。 C++ 标准规定 (C++03 §6.7):

可以转移到块中,但不能以绕过初始化声明的方式。从具有自动存储持续时间的局部变量不在范围内的点跳转到其在范围内的点的程序是格式错误的,除非该变量具有 POD 类型 (3.9)并且在没有初始化器的情况下声明(8.5)。

(*) 从 switch 语句的条件到 case 标签的转移在这方面被视为跳转。

int newVal = 42; 是一个具有初始值设定项的声明(= 42 部分)。该程序格式不正确,因为如果 val12,您将跳到初始化之后的 switch 块。

int newVal2; 也是一个声明;因为 int 是 POD 类型并且该声明没有初始值设定项,因此您可以跳过该声明。

Effectively, the rule is that you can't jump into a block past a declaration that has an initialization (or past the declaration of a non-POD type variable). The C++ standard says (C++03 §6.7):

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps(77) from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5).

(*) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.

int newVal = 42; is a declaration that has an initializer (the = 42 part). The program is ill-formed because if val is 1 or 2, you'll jump into the switch block past the initialization.

int newVal2; is also a declaration; because int is a POD type and the declaration has no initializer, you can jump past this declaration.

唔猫 2024-10-01 06:09:32

事实上,两者都不是合法的 C++。你不能在 switch case 中声明一个变量,除非它是有作用域的:

switch(val)  
{  
case 0:  
  {
    int newVal = 42;  // now valid
  }
  break;
case 1:  
  {
    int newVal2;      // still Valid
    newVal2 = 42;  
  }
  break;
case 2:
  break;
}

你的编译器允许 case 1 的事实是你的编译器的一个缺陷,或者可能是一个扩展。至少,按照标准。

In fact, neither are legal C++. You cannot declare a variable in a switch case unless it is scoped:

switch(val)  
{  
case 0:  
  {
    int newVal = 42;  // now valid
  }
  break;
case 1:  
  {
    int newVal2;      // still Valid
    newVal2 = 42;  
  }
  break;
case 2:
  break;
}

The fact that your compiler permits case 1 is a defect of your compiler, or possibly an extension. At least, according to the standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文