Just looking at this code, we think the return value should be 1,
I would say that an experienced C ++ developer immediately noticed that something was not right and quickly came to the conclusion that some other programmer accidentally tried to use a comma:,
but as a result of execution returns 3.
No, the code should not be compiled because the case statement is not constant
And in fact, it does not compile in any intermediate modern compiler. For example, MSVC 2013 says:
stackoverflow.cpp(8) : error C2051: case expression not constant stackoverflow.cpp(10) : error C2051: case expression not constant
An expression of type 1, 2 is a comma operator application, and a comma operator implies that the expression is not a compile-time constant.
At least until C ++ 11 agreed and relaxed the rules to allow compilation of adding parentheses, i.e. case (1, 2): It just wonβt do what you expect.
This is partially true in several cases on the switch:
How so? This other question and answers are almost exclusively about C #, not about C ++.
I would like to know why the incorrect form compiles without errors or warnings about events (at least in the Borland C ++ compiler).
Because the compiler is too old. Better get a new one.
source share