简体   繁体   English

为什么运算符`?:`没有优先级?

[英]Why operator `?:` does not have priority?

After debugging I found that ternary operator ?: does not have priority. 调试后我发现三元运算符?:没有优先级。 My question is why? 我的问题是为什么?
I have the following code: 我有以下代码:

bool T = true;
cout << ((T == true) ? "true" : "false") << endl;
cout << (T == true) ? "true" : "false";

Output: 输出:

true
1

live demo: http://ideone.com/Tkvt9q 现场演示: http//ideone.com/Tkvt9q

The conditional operator does have a precedence (albeit slightly complicated by its ternary nature); 条件运算符确实具有优先权(尽管其三元性质略微复杂); but that precedence is very low. 但这个优先级非常低。 Since it's lower than << , the second is parsed as 因为它低于<< ,所以第二个被解析为

(cout << (T == true)) ? "true" : "false";

streaming the boolean value of T == true , then evaluating (but ignoring) the expression "true" . 流式传输T == true的布尔值,然后评估(但忽略)表达式"true" Most compilers will give a warning, if you enable a sensible set of warnings. 如果您启用一组合理的警告,大多数编译器都会发出警告。

Here is a reference to the operator precedences, showing << with a higher precedence (7) than ?: (15): http://en.cppreference.com/w/cpp/language/operator_precedence 以下是对运算符优先级的引用,显示<<具有更高优先级(7)而不是?: 15): http//en.cppreference.com/w/cpp/language/operator_precedence

The conditional operator ? : 条件运算符? : ? : does have precedence - it's number 15 in this table , lower than << and >> operators, which are number seven. ? :确实有优先级 - 它在这个表中的数字是15,低于<<>>运算符,它们是数字7。

The rule that I follow to avoid mistakes like that is to use parentheses when in doubt. 我遵循以避免这样的错误的规则是在有疑问时使用括号。 The rationale is simple: if you need to look up the priority in a table, good chances are that the readers of your code, including yourself, would need to do the same thing at some later time. 理由很简单:如果你需要在表格中查找优先级,很可能你的代码的读者,包括你自己,需要在以后做同样的事情。

A lot of the other questions answer why you are seeing that behavior but they don't answer why the ternary operator has low priority. 很多其他问题都回答了为什么你会看到这种行为,但他们没有回答为什么三元运算符的优先级低。

The decision was made to have low ternary priority because we don't want the code 由于我们不想要代码,因此决定具有较低的三元优先级

a<d ? 10 : 100

to end up meaning 结束意义

a < (d ? 10 : 100) //BAD: not what we normally expect

we want it to mean 我们想要它的意思

(a<d) ? 10 : 100 

Low priority for the ternary operator achieves this goal. 三元运算符的低优先级实现了这一目标。 This sort of thing is the whole reason for operator precedence that you find in languages. 这种事情是您在语言中找到的运算符优先级的全部原因。 The goal is to make it convenient to write expressions that are expected to be normal in the language. 目标是方便编写预期在语言中正常的表达式。 Right? 对? If not, it could just be left to right with parentheses. 如果没有,它可以用括号左右。 Which would be annoying to use and you'd quickly propose some sort of convenient operator precedence. 这对使用起来很烦人,你很快就会提出一些方便的运算符优先级。

It is true that the ?: operator has no clearly defined priority. 确实, ?:运算符没有明确定义的优先级。 But the example in question does not really illustrate that. 但是这个例子并没有真正说明这一点。 The relative priorities of << and ?: are rather unambiguous. <<?:的相对优先级是相当明确的。 All your example shows is that the priority of << is higher than the priority of ?: . 您的所有示例都显示<<的优先级高于?:的优先级?:

As for the more general issue of the priority of ?: operator... ?: operator has relatively convoluted format, compared to other operators: it has three non-uniform operands. 至于更优先的问题是?: operator ... ?:运算符与其他运算符相比具有相对复杂的格式:它有三个非均匀操作数。 Because of that non-uniformity the part before ? 由于之前的那部分不均匀? has different syntactic grouping properties than the parts after ? 具有不同的句法分组属性而不是之后的部分? .

After all, "priorities" are a derivative trick invented to simplify visualization and memorization of syntactic groupings defined by the grammar. 毕竟,“优先级”是为了简化语法定义的句法分组的可视化和记忆而发明的衍生技巧。 Not all C++ operators conform to that trick though. 但并非所有C ++运算符都符合该技巧。 The ?: operator happens to be the one that does not, which is why a properly written priority table will typically have a side note for ?: operator, explaining its unusual properties. ?:运算符恰好是那个没有的运算符,这就是为什么正确编写的优先级表通常会为?:运算符提供旁注,解释其不寻常的属性。 Again, the part before the ? 再次,之前的部分? has different priority than the parts after the ? 有不同的优先级后的部分? , which is why it is impossible to properly place the ?: operator into a linear table of priorities. 这就是为什么不能将?:运算符正确地放入线性优先级表中的原因。

Unless I'm forgetting something, the ?: operator is the only operator without a straightforwardly definable priority. 除非我忘了什么,否则?:运算符是唯一没有直接可定义优先级的运算符。

PS Things with ?: operator were even worse in C. C++-related changes to the grammar made ?: to conform better to the idea of linear priority. 带有?:运算符的PS事物在C语言中更糟糕.C ++与语法相关的变化是?:更符合线性优先级的想法。

Like the other answers note, the << operator has higher precedence than the ? 像其他答案一样, <<运算符的优先级高于? operator. 运营商。 Here is my opinion on why it is so. 以下是我对其原因的看法。

Using bit-shift ( << ) with the choice operator makes sense; 使用选择运算符的位移( << )是有意义的; for example 例如

(T == true) ? (whatever << 1) : (whatever << 2)
(T == true) ? whatever << 1 : whatever << 2 // same as above

When the designers of C++ invented operator overloading, they decided to reuse the bit-shift operator << as a streaming operator. 当C ++的设计者发明了运算符重载时,他们决定重复使用位移运算符<<作为流操作符。 Had they introduced another, completely new streaming operator (eg <<< ), it would get very low precedence, so the code would work as expected: 如果他们引入了另一个全新的流媒体运营商(例如<<< ),它将获得非常低的优先级,因此代码将按预期工作:

cout <<< (T == true) ? "true" : "false";

However, introducing a new operator just for it to be overloaded would undermine the idea of usefulness of operator overloading, which was a fascinating idea back then. 然而,引入一个新的运算符只是为了让它过载会破坏运算符重载的有用性的想法,这是一个令人着迷的想法。 So they decided to use an old (existing) operator. 所以他们决定使用旧的(现有的)运营商。

The ternary conditional operator does have a precedence, except that it's at the bottom of the list (value 15). 三元条件运算符确实具有优先权,除了它位于列表的底部(值15)。 Only throw and , (comma) have a lesser precedence. 只有throw, (逗号)具有较小的优先级。 See this reference for C++ Operator Precedence . 有关C ++ Operator Precedence的信息,请参阅此参考。

because of brackets, firstly statement (T == true) is evaluated, then comes the left shift operator and 1 which was evaluated earlier is sent to cout. 因为括号,首先评估语句(T == true) ,然后左移位运算符和先前评估的1被发送到cout。 later, the ternary operator evaluates "true" but it is ignored. 之后,三元运算符计算"true"但忽略它。

all in all, ternary operator has precedence, but one of the lowest. 总而言之,三元运算符具有优先权,但是最低的运算符之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM