Recently, I encountered a strange compiler error at work. I was trying something similar to this:
but, it didn’t compile. So, I played my Jedi debugging tricks to see
what exactly went wrong?
To my surprise,
Now, it was well clear that
() was the pith of the error.
To strengthen my understanding, I tried some more cases with
1 2 3
The results were affirmative. So, I went to check the C++ Standard.
The syntax specification in 6.4/1 gives the following for selection condition:
1 2 3 4 5 6 7 8
The condition in an if or while statement can be either an expression, or a single variable declaration (with initialisation).
The failing examples are neither valid statements, nor valid declarations, since a declaration can’t form part of an expression. The syntax specification says a single declaration, with no parentheses or other adornments and they don’t fit the “either expression or declaration” rule for the condition.
So, to sum it up, the rules for variable declaration in a selection-statement are:
- can only have 1 variable declared per expression,
- the variable declaration must occur first in the expression and
- can’t have parenthesis around declaration ( as per the syntax specification)
Q. Why the variable declaration must occur first, and why can we have only 1 variable declared?
A: Short-circuit evaluation of logic and the declared variable(s) scope. Consider this case:
&& operator is a short-circuit logical AND. As the first part 0 is false, then the second part
(int a = val) shouldn’t be evaluated because the final answer will be false. If it isn’t evaluated, then what to do with code later on that uses a? Should we initialize the variable and leave it undefined?
For these reasons, the variable declaration must be first in the expression and the rules for variable declaration in a selection-statement stated above hold.