我一直在想这个问题——为什么不能在switch语句的case标签后面声明变量呢?在c++中,你可以在任何地方声明变量(并且在第一次使用时声明它们显然是一件好事),但以下仍然不能工作:

switch (val)  
{  
case VAL:  
  // This won't work
  int newVal = 42;  
  break;
case ANOTHER_VAL:  
  ...
  break;
}  

上面给了我以下错误(MSC):

'newVal'的初始化被'case'标签跳过

这在其他语言中似乎也是一个限制。为什么这是一个问题?


当前回答

新变量只能在块范围内声明。你需要这样写:

case VAL:  
  // This will work
  {
  int newVal = 42;  
  }
  break;

当然,newVal只在大括号内有作用域……

干杯,拉尔夫

其他回答

有趣的是,这很好:

switch (i)  
{  
case 0:  
    int j;  
    j = 7;  
    break;  

case 1:  
    break;
}

... 但这不是:

switch (i)  
{  
case 0:  
    int j = 7;  
    break;  

case 1:  
    break;
}

我得到一个修复足够简单,但我不明白为什么第一个例子不打扰编译器。正如之前所提到的(2年前的呵呵),声明不是导致错误的原因,即使有逻辑。初始化是问题所在。如果变量在不同的行上被初始化和声明,它将被编译。

这个问题的答案是我写的。然而,当我完成它,我发现答案已经关闭。所以我把它贴在这里,也许喜欢参考标准的人会发现它很有用。

问题的原始代码:

int i;
i = 2;
switch(i)
{
    case 1: 
        int k;
        break;
    case 2:
        k = 1;
        cout<<k<<endl;
        break;
}

实际上有两个问题:

1. 为什么我可以在case标签后声明一个变量?

这是因为在c++中标签必须是这样的:

N3337 6.1/1

标记语句: … 属性说明符-seqopt case常量表达式:语句 …

在c++中声明语句也被认为是语句(与C相反):

N3337 6/1:

声明: ... 说明语句 ...

2. 为什么我可以跳过变量声明,然后使用它?

因为: N3337 6.7 / 3

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps (The transfer from the condition of a switch statement to a case label is considered a jump in this respect.) from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5).

因为k是标量类型,并且在声明时没有初始化,跳过它的声明是可能的。这在语义上是等价的:

goto label;

int x;

label:
cout << x << endl;

然而,如果x在声明点初始化,这将是不可能的:

 goto label;

    int x = 58; //error, jumping over declaration with initialization

    label:
    cout << x << endl;

好的。我要澄清一下,这和申报完全没有关系。它只涉及“跳过初始化”(ISO c++ '03 6.7/3)

这里的许多帖子都提到跳过声明可能会导致变量“未被声明”。这是不对的。POD对象可以在没有初始化式的情况下声明,但它将具有不确定的值。例如:

switch (i)
{
   case 0:
     int j; // 'j' has indeterminate value
     j = 0; // 'j' set (not initialized) to 0, but this statement
            // is jumped when 'i == 1'
     break;
   case 1:
     ++j;   // 'j' is in scope here - but it has an indeterminate value
     break;
}

当对象是非pod或聚合时,编译器会隐式地添加一个初始化式,因此不可能跳过这样的声明:

class A {
public:
  A ();
};

switch (i)  // Error - jumping over initialization of 'A'
{
   case 0:
     A j;   // Compiler implicitly calls default constructor
     break;
   case 1:
     break;
}

这种限制并不局限于switch语句。使用'goto'跳过初始化也是一个错误:

goto LABEL;    // Error jumping over initialization
int j = 0; 
LABEL:
  ;

一个小细节是,这是c++和C之间的区别。在C中,跳过初始化并不是错误。

正如其他人所提到的,解决方案是添加一个嵌套块,以便变量的生命周期被限制为单个case标签。

整个switch语句在同一个作用域中。要解决这个问题,可以这样做:

switch (val)
{
    case VAL:
    {
        // This **will** work
        int newVal = 42;
    }
    break;

    case ANOTHER_VAL:
      ...
    break;
}

注意括号。

试试这个:

switch (val)
{
    case VAL:
    {
        int newVal = 42;
    }
    break;
}