I have read this "transition to case" error question , but I still have some questions. I am using g ++ 4.7 on Ubuntu 12.04.
This code gives an error:
int main() { int foo = 1; switch(foo) { case 1: int i = 0; i++; break; case 2: i++; break; } }
Error
jump-to-case-label.cpp: In function 'int main()': jump-to-case-label.cpp:8:8: error: jump to case label [-fpermissive] jump-to-case-label.cpp:5:9: error: crosses initialization of 'int i'
However, this code compiles fine,
int main() { int foo = 1; switch(foo) { case 1: int i; i = 0; i++; break; case 2: i++; break; } }
Is the second code less dangerous than the first? I am confused why g ++ allows this.
Secondly, the fix for this problem is to span the initialized variable. If the initialized variable is a large object, and the switch statement is in the while loop, will the constructor and destructor be called every time the input area is entered and left, which leads to a decrease in efficiency? Or does the compiler optimize this?
source share