Stefan Ram wrote:
Quote:
I have devised two tests for learners of C++:
>
1.) When you want to test whether something is an
expression within a correct program, put a pair of
parentheses around it. If it is an expression, the
program will still be correct and the behavior of the
program will not change.
Hm... I'm not sure about the intended scope of this rule. Say in this
declaration
int i;
I can put a pair of parentheses around 'i'
int (i);
and get an equivalent program. This still doesn't mean that this
specific 'i' in this specific context is an expression. It is not. Sure,
out of context 'i' is a valid expression, but in the above context it is
a declarator.
BTW, is your rule supposed to work both ways? I.e. if one can't put the
braces around it, does that mean it is not an expression? If so, then it
is not true as well. For example, in this context
struct A { int i; };
struct B : A {
void foo() {
int A::*p = &A::i; // 'A::i' is an expression in this context
}
};
'A::i' is an expression. However, if I put braces around it as in
struct B : A {
void foo() {
int A::*p = &(A::i); // ERROR
}
};
the program becomes ill-formed.
Quote:
2.) When you want to test whether something is a statement
within a correct program, put a pair of braces around it.
If it is a statement, the program will still be correct
and the behavior of the program will not change.
In C++ (as opposed to C, for example) declarations form declaration
_statements_. Juha already gave you an counterexample where extra braces
break the code.