Does the C/C++ ternary operator actually have the

2019-01-19 16:49发布

Almost all C/C++ operator precedence tables I have consulted list the ternary conditional operator as having higher precedence than the assignment operators. There are a few tables, however, such as the one on wikipedia, and the one at operator-precedence.com, that place them on the same precedence level. Which is it, higher or same?

3条回答
2楼-- · 2019-01-19 16:54

You'll find that, in the standard:

5 Expressions [expr]

58) The precedence of operators is not directly specified, but it can be derived from the syntax. (note)

This means precedence tables are inferred, not specified. As long as they behave the same, you can say that both are right. So, even if a precedence table places them as having the same precedence, or places the ternary above the assignment operator, in practice the same thing happens, because of the syntax.

Note that associativity plays a bigger role here (this is also derived from the syntax).

Even if you assume that they have the same precedence:

a = b ? c : d;

will be treated as a = (b ? c : d) because they are both right-to-left associative.

查看更多
该账号已被封号
3楼-- · 2019-01-19 17:11

In the C++ grammar,

assignment-expression:
    conditional-expression
    logical-or-expression assignment-operator initializer-clause
    throw-expression

conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression

initializer-clause:
    assignment-expression
    braced-init-list

could be combined to

assignment-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression
    logical-or-expression assignment-operator assignment-expression
    logical-or-expression assignment-operator initializer-clause
    throw-expression

If only looking at = and ?:, and if ignoring the inner expression between ? and :, this clearly gives ?: and = the exact same precedence.

This is different from the C grammar, in which neither ?:'s left nor its right operand can have an assignment operator as its topmost operator.

assignment-expression:
    conditional-expression
    unary-expression assignment-operator assignment-expression

conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

So for C, it makes sense to give them different precedence levels.

That said, precedence levels are only an approximation of what the standard actually says, there will be cases for any precedence levels you choose that show the levels to be misleading or just plain wrong. Depending on your interpretation, the inner expression of ?: may be one of them, it is for me.

查看更多
太酷不给撩
4楼-- · 2019-01-19 17:14

The answer for C++ is that ?: and = have the same precedence. Yes, almost every C++ operator precedence table out there is wrong.

In C it doesn't matter whether ?: is higher than = or not, because in C the ?: operator is not allowed to evaluate to an l-value, which is what it would have to do if precedence were to influence the behavior (given that they are already RTL associative). See the discussion under Luchian Crigore's answer for example.

Perhaps this error is so widespread because early C++ operator precedence tables may have been copied and extended from C tables. And perhaps the error has persisted because the only counterexample - expressions of the form a?b:c=d - are rarely used. Perhaps.

查看更多
登录 后发表回答