C ++优先级权威列表 [英] C++ Precedence definitive list

查看:101
本文介绍了C ++优先级权威列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速搜索C ++优先级会产生许多尝试.令人不安的部分是它们都是不同的.当然,大多数细节都是错误的,尽管细节很少.

A quick search for C++ precedence yields many attempts. The disconcerting part is that they are all different. Most are assuredly wrong, albeit in minor details.

我将包括三个.来自cppreference.com的第一个声称有16个优先级. Learn.cpp有18. 普渡大学的一张简单的桌子要简单得多.

I will include three. The first, from cppreference.com claims there are 16 levels of precedence. Learn.cpp has 18. A simpler table at university of Purdue is much simpler.

http://en.cppreference.com/w/cpp/language/operator_precedence

http://www.learncpp.com/cpp-tutorial /31-precedence-and-associativity/

http://web.ics.purdue.edu/~cs240/misc/operators.html

这些都不是确定的,但是当我查看ISO标准草案时,

None of these is definitive, but when I looked at the draft ISO standard, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf it did not even have a table, stating that you could figure it out from context.

我的问题是1)在任何地方都可以找到关于优先级的良好,确定的摘要?

My questions are 1) is there a good, definitive summary of precedence anywhere?

2)谁能评论这些表格传达的所有不同事实"?

2) can anyone comment on all the differing "facts" conveyed by these tables?

例如,前两个同意后加/减与括号处于同一水平,这在实践中意味着什么?

For example, the first two agree that postincrement/decrement is on the same level as parentheses, what does that mean in practice?

(x+1)++

毫无意义,因为x + 1是rexpr,并且显然用括号括起来的表达式使后增量无效.

makes no sense because x+1 is an rexpr, and obviously parentheses which bracket an expression invalidate postincrement.

(*p)++;

我已经读到后增量要比前增量高,所以我将其放在()[]等下方.是什么使它相等(如果是)?

I have read that postincrement is higher than preincrement, so I would put it just below () [] etc. What makes it equal (if it is)?

cppreference声明该throw与赋值运算符处于同一级别.这显然是错误的,因为:

cppreference claims that throw is on the same level as assignment operators. This seems patently false, since:

throw x += 5;

大概应该在投掷之前先计算x + = 5. learningcpp有所不同,第三个消息源根本没有提到throw作为运算符.

should presumably first compute x += 5 before throwing. learncpp differs, and the third source doesn't mention throw as an operator at all.

这是我第一次看到操作员.我承认多年来没有研究标准,但是回报是声明,为什么不抛出?

This is the first time I have ever seen as an operator. I admit to not studying the standard for years, but return is a statement, why not throw?

任何说明优先级表的注释在这里都很棒.

Any comments illuminating bits of the precedence table would be great here.

推荐答案

优先级不是语言规范的一部分.这是人类(人类)用来理解表达式含义而无需像实际解析器那样对它进行递归分析的助记符设备.

Precedence is not a part of the language specification. It is a mnemonic device used by us, humans, to understand the meaning of an expression without recursively analyzing it like an actual parser.

throw,?:和C ++中的所有赋值都是语法生成 assignment-expression 中的替代项,定义如下

throw, ?: and all assignments in C++ are alternatives in the grammar production assignment-expression, defined as follows

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

条件表达式定义为逻辑或表达式 ? 表达式 : 赋值表达式初始化器子句是另一个赋值表达式(当它不是 branch-init- list ),并且 throw-expression 被定义为关键字throw,后跟可选的assignment-expression.

Where conditional-expression is defined as logical-or-expression ? expression : assignment-expression, initializer-clause is another assignment-expression (when it isn't a braced-init-list), and throw-expression is defined as the keyword throw followed by an optional assignment-expression.

用人类的术语来说,这被描述为相同的优先级,从右到左分组".

In human terms, we describe this as "same precedence, grouping right to left".

要重用 cppreference 示例,

e = a < d ? a++ : a = d解析为e = ((a < d) ? (a++) : (a = d))

false ? 7 : throw 3解析为false ? 7 : (throw 3),是的,您的throw x += 5示例解析为throw (x += 5)

and false ? 7 : throw 3 parses as false ? 7 : (throw 3), and yes, your example of throw x += 5 parses as throw (x += 5)

这篇关于C ++优先级权威列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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