C ++主表达式 - 它是主表达式还是不是? [英] C++ primary expressions - Is it primary expression or not?

查看:511
本文介绍了C ++主表达式 - 它是主表达式还是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)为什么他们被称为主要?按照评价顺序,它们是第一个?



2)C ++ 03标准定义了第5章(注1)中的表达式:一个表达式是

$ b

然后,5.1Primary expressions定义了主表达式的列表:



(1)primary-expression:



文字



(表达式)



id-expression



是连接的第三个点:



(表达式)



所以,根据标准,括号是初始表达式,首先计算它们。它看起来符合逻辑,并给出了C ++表达式中括号的行为的精确解释(优先级)。



所以这个例子,例如



(变量+ 10)



是主要表达式。



var =变量+10)* 3



根据我的理论,它看起来是逻辑,但是从其他来源我知道



(variable + 10)



不是主要表达式,而是为什么?我不明白,但是标准定义



(表达式)



作为主表达式。 / p>

请帮助我,因为我不能。非常感谢,对不起我的英语。
Hi。

解决方案

C ++表达式可能很复杂,也就是说它们可以由嵌套表达式组成,



如果你将一个复杂的表达式分解成更小的单位,在某些时候,你将会留下原子的单位,在它们不能进一步分解的意义上。这些是初级表达式;它们包括标识符,文字,关键字 this 和lambda表达式。



C ++标准定义为主要的一个非原子结构:括在圆括号(也称为括号)中的表达式。所以你给出的(variable + 10)例子是一个主表达式(子表达式 variable 这是一个标识符), 10 (这是一个文字)。



主表达式,因为它们在评估顺序中扮演真正的原子表达式的角色:括号中的任何东西都必须在返回的表达式的值可以进入具有其他表达式的评估之前进行求值:In (5 + 10)* a ,必须评估 5 + 10 的值,才能进入 * a 。[注意,这并不意味着 5 + 10 在表达式 a 被评估,它只意味着 5 + 10 必须在乘法本身被评估之前被评估。]



因此,括号中的子表达式,在这个意义上,就像它们是原子的。



我想这就是为什么标准不使用术语原子表达式。他们的行为好像是原子的,但至少括号内的品种实际上并不是原子的。 小学似乎对我来说是一个很好的选择。


1) Why are they called "primary"? In the order of evaluence they are the first?

2) C++03 standard defines the expression in chapter 5 (Note 1): "An expression is a sequence of operators and operands that specifies a computation."

Then the 5.1 "Primary expressions" defines the list of primary expressions:

(1) primary-expression:

literal

this

( expression )

id-expression

My main question is in connection the thrid point:

( expression )

So, according to the standard, every expression with brackets are primary expressions and they are calculated firstly. It looks logical, and gives an exact explanation of the behaviour of brackets in C++ expressions (precedence).

So this menas that for example

(variable + 10)

is a primary expression.

var = (variable + 10) * 3

and according to my theory, it looks logic, BUT from other sources I know

(variable + 10)

is NOT a primary expression, but WHY? I don't understand, however the standard defines the

(expression)

as a primary expression.

Please, help me because I can't. Thank you very much, and sorry for my bad English. Hi.

解决方案

C++ expressions can be complex, which is to say they can be made up of nested expressions, combined through the use of operators, and those nested expressions may in turn be complex.

If you decompose a complex expression into ever smaller units, at some point you'll be left with units that are atomic in the sense that they cannot be decomposed further. Those are primary expressions; they include identifiers, literals, the keyword this, and lambda expressions.

However, it is true that there is one non-atomic construct that the C++ Standard defines as primary: Expressions enclosed in round brackets (aka parentheses). So the (variable + 10) example you give is a primary expression (and so are the sub-expressions variable (which is an identifier), and 10 (which is a literal).

I believe the Standard lists them as primary expressions because they play the some role as truly atomic expressions when it comes to the order of evaluation: Anything within the brackets must be evaluated before the value of the backeted expressions can enter into evaluations with other expressions: In (5+10)*a, the value of 5+10 must be evaluated before it can enter into the evaluation of *a. [Note that this does not mean 5+10 is evaluated before the expression a is evaluated. It only means that 5+10 must be evaluated before the multiplication itself can be evaluated.]

So, bracketed sub-expressions, in this sense, act as if they were atomic.

And I guess this is why the Standard doesn't use the term "atomic expressions" for this concept. They act as if they were atomic, but at least the bracketed variety is not actually atomic. "Primary" seems, to me, to be a good choice of words.

这篇关于C ++主表达式 - 它是主表达式还是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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