Why is this ok:
if(int i = 1) {
}
...whereas the following produces errors?
if((int i = 1)) {
}
Under g++ (4.4.5) the latter gives:
test.cpp:7: error: expected primary-expression before ‘int’
test.cpp:7: error: expected ‘)’ before ‘int’
test.cpp:9: error: expected ‘)’ before ‘else’
test.cpp:13: error: expected primary-expression before ‘}’ token
test.cpp:13: error: expected ‘;’ before ‘}’ token
Incidentally, the reason I'm asking is because of this answer: Seeing what class an object is
I'm trying to find a way to make the condition more readable. Usually, I would prefer, for example:
if((x = y) != 0) {
to
if(x = y) {
...since it's more readable and silences compiler 'comments' suggesting I might have used the wrong operator. If I'm using a declaration as a condition, it doesn't produce the warning, but the readability still seems to suffer.