Switch case variable scope
Ran into something today that does not make sense:
This compiles:
Code:
int x = 5;
switch(x)
{
case 0:
{
int value = 5;
}
break;
case 1:
{
int value = 6;
}
break;
}
This does not:
Code:
int x = 5;
switch(x)
{
case 0:
{
int value = 5;
}
break;
case 1:
int value = 6;
break;
}
Ok so it doesn't like int value = 6 b/c of int value = 5 for case 0. However since the value in case 0 is declared within the brackets one would think it has case scope.
So I tried this:
Code:
int x = 5;
switch(x)
{
case 0:
{
int value = 5;
}
break;
case 1:
value = 6;
break;
}
Now it doesn't like it b/c value has not been declared in case 1:. These two conditions cannot possibly be both true at the same time. You cannot disallow the declaration of value in case 1 b/c it interferes with value in case 0 and at the same time disallow me to use value from case 0 b/c it is not in scope. If it was not in scope then theoretically I should be able to declare value in case 1.
Both MSVS 2012 and 2013 exhibit the same behavior. I checked the standard and it is unclear on the matter.
Any explanations?