A
Andrew Koenig
It has been pointed out to me that various C++ books disagree about
the relative precedence of ?: and the assignment operators.
In order to satisfy myself about the matter once and for all,
I looked at the grammar in the C++ standard. The relative fragments
are as follows:
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
assignment-expression:
conditional-expression
logical-or-expression assignment-operator assignment-expression
throw-expression
Ordinarily, this grammar is almost, but not quite, in a well-known
form that, were it adhered to exactly, would say that
conditional-expressions have higher precedence than
assignment-expressions.
However, there is one crucial difference from this form: The rightmost
token in the second line of "conditional-expression" is
"assignment-expression" rather than "conditional-expression".
This difference makes the grammar somewhat harder to understand in
terms of precedence.
However, if we take the definition of assignment-expression and
replace the use of conditional-expression by its alternatives, we get:
assignment-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
logical-or-expression assignment-operator assignment-expression
throw-expression
which we can rewrite this way:
cond-or-asn-op:
assignment-operator
? expression :
assignment-expression:
logical-or-expression
logical-or-expression cond-or-asn-op assignment-expression
throw-expression
From this rewrite, it should be clear that the assignment and ?:
operators have the same precedence, and they are right-associative.
Am I missing anything? If not, I'd like to urge all authors and
teachers of C++ to describe the precedence of these operators this
way; I think it's much easier to understand than any alternatives I've
seen.
the relative precedence of ?: and the assignment operators.
In order to satisfy myself about the matter once and for all,
I looked at the grammar in the C++ standard. The relative fragments
are as follows:
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
assignment-expression:
conditional-expression
logical-or-expression assignment-operator assignment-expression
throw-expression
Ordinarily, this grammar is almost, but not quite, in a well-known
form that, were it adhered to exactly, would say that
conditional-expressions have higher precedence than
assignment-expressions.
However, there is one crucial difference from this form: The rightmost
token in the second line of "conditional-expression" is
"assignment-expression" rather than "conditional-expression".
This difference makes the grammar somewhat harder to understand in
terms of precedence.
However, if we take the definition of assignment-expression and
replace the use of conditional-expression by its alternatives, we get:
assignment-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
logical-or-expression assignment-operator assignment-expression
throw-expression
which we can rewrite this way:
cond-or-asn-op:
assignment-operator
? expression :
assignment-expression:
logical-or-expression
logical-or-expression cond-or-asn-op assignment-expression
throw-expression
From this rewrite, it should be clear that the assignment and ?:
operators have the same precedence, and they are right-associative.
Am I missing anything? If not, I'd like to urge all authors and
teachers of C++ to describe the precedence of these operators this
way; I think it's much easier to understand than any alternatives I've
seen.