I agree with MHotchin's statement above "It's nice that the compiler tried to tell you about it, but it's not required to do so, and telling you about it does NOT make the behaviour well defined.".
Anyway, the Visual Studio 2019 compiler (with Visual Micro Arduino IDE for Visual Studio v1912.28.2) didn't throw an error or warning - and I understand now this is OK and it does not violate a standard (although it's a pitty).
For me, the mystery is resolved. Thanks !
There is no "Visual Studio compiler". VisualMicro uses not only the compiler, but the entire Arduino build system from the Arduino IDE to which is it linked by the configuration you setup when you installed the VisualMicro plug-in. VisualStudio contains no compiler, and neither knows, nor cares, what compiler is used.
Regards,
Ray L.
OK - just read "... from inside the Visual Studio environment ...".
But you are right and I appreciate the clarification.
arduino_new:
Undefined means that it is not defined by the standard. If the standard defined it (in this case, the program is ill-formed which is an error), then the behavior is well-defined.
OK, I read through the standard, and you're technically correct, but this is just splitting hairs (IMO). A compiler is not actually required to compile a program that is ill-formed, because the program violates one or more syntactic or semantic rules of the standard. g++ can and will compile a program that jumps over the initialization of a variable, even though that is ill-formed, but the program won't necessarily do what the programmer "expects", which is in the same neighborhood as "undefined behavior".
TL;DR: Whether it's ill-formed or undefined behavior, the program violates the standard, so all bets are off. The compiler even gives a warning about the violation (or it should give a warning, because the standard requires the compiler to do so). Heed the warning!
ToddL1962:
What Bob said. The compiler is trying to protect you from scoping/initialization issues.
The issue has to do with the case statements being labels, the scope of the variable you are defining, and initialization of the variable. It is not worth drowning in the gory details so the put the declaration in a code block in order to limit the scope of the variable and ensure proper initialization.
And for some reason someone thought my opinion about getting into the gory details was "curious".
christop:
OK, I read through the standard, and you're technically correct, but this is just splitting hairs (IMO). A compiler is not actually required to compile a program that is ill-formed, because the program violates one or more syntactic or semantic rules of the standard. g++ can and will compile a program that jumps over the initialization of a variable, even though that is ill-formed, but the program won't necessarily do what the programmer "expects", which is in the same neighborhood as "undefined behavior".
TL;DR: Whether it's ill-formed or undefined behavior, the program violates the standard, so all bets are off. The compiler even gives a warning about the violation (or it should give a warning, because the standard requires the compiler to do so). Heed the warning!
By the standard, cross initialization is an error. GNU/clang/VSC all treat it as an error (because it was defined by the standard as an error). Now, I'm not familiar with all the Arduino version but my 1.8.9 treats it as an error also.