内容简介:我所咨询的几乎所有C/C++运算符优先级表都列出了三元条件运算符具有比赋值运算符更高的优先级.但是,有一些表,例如翻译自:https://stackoverflow.com/questions/13515434/does-the-c-c-ternary-operator-actually-have-the-same-precedence-as-assignment
我所咨询的几乎所有C/C++运算符优先级表都列出了三元条件运算符具有比赋值运算符更高的优先级.但是,有一些表,例如 wikipedia 上的表和 operator-precedence.com 上的表,它们将它们放在相同的优先级上.它是更高还是更高?
在C语法中,
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
可以结合起来
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
如果只看=和?:,如果忽略内部表达式?并且:这显然给出了?:和=完全相同的优先级.
这与C语法不同,在C语法中,?:的左边或右边的操作数都不能将赋值运算符作为其最顶层的运算符.
assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression conditional-expression: logical-OR-expression logical-OR-expression ? expression : conditional-expression
因此对于C来说,赋予它们不同的优先级是有意义的.
也就是说,优先级只是标准实际所说的近似值,您选择的任何优先级都会出现这种情况,表明这些级别具有误导性或者是完全错误的.根据你的解释,?:的内在表达可能是其中之一,它适合我.
翻译自:https://stackoverflow.com/questions/13515434/does-the-c-c-ternary-operator-actually-have-the-same-precedence-as-assignment
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Elements of Statistical Learning
Trevor Hastie、Robert Tibshirani、Jerome Friedman / Springer / 2009-10-1 / GBP 62.99
During the past decade there has been an explosion in computation and information technology. With it have come vast amounts of data in a variety of fields such as medicine, biology, finance, and mark......一起来看看 《The Elements of Statistical Learning》 这本书的介绍吧!