KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Why does this code fragment run fine void foo(int i) { switch(i) { case 1: { X x1; break; } case 2: X x2; break; } } whereas the following gives compilation error (initialization of 'x1' is skipped by 'case' label)? void foo(int i) { switch(i) { case 1: X x1; break; case 2: X x2; break; } } I understand that using braces introduces a new scope, hence storage will not be allocated for x1 till we hit its opening brace. But x2 is still initialized inside a case label without enclosing braces. Should this not be an error too? I think initialization of x2 can be conditionally skipped in both the code fragments
Tags (comma-separated)
Save Edits
Cancel