请问C / C ++三元运算符实际上具有相同的precedence作为赋值运算符? [英] Does the C/C++ ternary operator actually have the same precedence as assignment operators?

查看:110
本文介绍了请问C / C ++三元运算符实际上具有相同的precedence作为赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎所有的C / C ++运算符precedence表我已征询名单三元条件运算符具有更高的precedence比赋值操作符。有几个表,但是,如一个在维基和一个在< A HREF =HTTP://cpp.operator-$p$pcedence.com/>操作符precedence.com ,那个地方他们在同一precedence水平。这是它,大于或相同?

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?

推荐答案

对于C答案++是 = 具有相同的precedence。是的,几乎每一个C ++运算符precedence表那里是错误的。

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

在C中,它无关紧要是否高于 = 或不是,因为在C 经营者不得计算为左值,这是它必须做的,如果precedence是影响的行为(因为它们是已经RTL联想)。参见下Luchian Crigore的回答例如讨论。

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.

也许这个错误是如此wides $ P $垫,因为早期的C ++运算符precedence表可能已经被复制,并从C表延长。也许是错误一直存在,因为唯一的反 - 形式的前pressions A B:C = D - 很少被使用。也许吧。

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.

这篇关于请问C / C ++三元运算符实际上具有相同的precedence作为赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆