为什么在这种情况下括号无法更改c ++运算符的优先级? [英] why parenthesis is unable to change c++ operator precedence in this case?

查看:57
本文介绍了为什么在这种情况下括号无法更改c ++运算符的优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的简单代码:

int main()
{
    int x = 5;
    cout << (x++) << endl;

    return 0;
}

上面的代码打印的是 5 ,而不是6,即使带括号,我想x = x + 1在打印出来之前先执行吗?谁能告诉我这是怎么回事?谢谢

the code above prints 5, not 6, even with the parenthesis, My thought is x = x + 1 be executed first before it is printed out? can anyone explain to me what's going on here? Thank you

我肯定了解++ x家伙,我的问题是关于使用()的更改运算符优先级

推荐答案

我肯定了解++ x家伙,我的问题是关于使用()的更改运算符优先级

i definitely understand ++x guys, my question is about change operator precedence using ()

运算符优先级与此无关.

Operator precedence has nothing to do with this.

误解可能不是您的错:您可能被误会了.您的老师告诉您,比其他操作数优先级更高的操作数将被首先执行".

The misunderstanding probably isn't your fault: you've likely been mistaught. Your teacher(s) told you that an operand with a higher precedence, than some other operand, will be "executed first".

虽然这是学校中的常见解释,但事实并非如此.

从这种意义上讲,有三件事可以改变表达式的含义:

There are three things that can change the meaning of an expression in this sense:

 

  1. 运算符优先级

这仅仅是一组规则,它们告诉我们并告诉编译器哪些操作数到达哪个运算符.就像在 3 + 5 * 7 中,我们将 3 + 5 传递给乘法运算符,还是将 5 * 7 传递给乘法器.加法运算符?关于解析.

This is merely a set of rules that tell us, and tell the compiler, which operands go to which operator. Like, in 3 + 5 * 7, do we pass 3+5 to the multiplication operator, or do we pass 5*7 to the addition operator? It's about parsing.

 

评估顺序

然后需要对每个操作数求值以产生一个值(例如, 3 + 5 变为 8 ,或者 5 * 7 变为 35 ).在C ++中,这些评估发生顺序的规则相当复杂,比您预期的要复杂得多,但通常不必担心它们,除非您在顺序点之间做疯狂的事情(借用预先C ++ 11的说法).

Each operand then needs to be evaluated to produce a value (e.g. 3+5 becomes 8, or 5*7 becomes 35). The rules on the order in which these evaluations happen are quite complicated in C++, more so than you might expect, but you usually don't have to worry about them unless you're doing crazy things in between sequence points (to borrow pre-C++11 parlance).

(这是最接近将首先执行"的概念.)

(This is the closest you'll get to a notion of "will be executed first".)

 

运算符的含义

这是您要停留在这里的地方.后缀增量运算符 x ++ 的含义是增量x,并求出旧值".时期.句号.

This is where you're coming unstuck here. The meaning of the postfix increment operator x++ is "increment x, and evaluate to the old value". Period. Full stop.

哪个运算符优先级规则导致对表达式 x ++ 的求值(与代码中符号的其他解释相反)无关紧要:当求值时, whenever 进行评估,您将获得 x 的旧值.

It doesn't matter which operator precedence rules led to the expression x++ being evaluated (as opposed to some other interpretation of the symbols in your code): when it's evaluated, whenever it's evaluated, you get the old value for x.

前缀增量运算符 ++ x 的含义是递增x,并求出新值",这就是您想要的行为,这就是您应该编写的代码.

The meaning of the prefix increment operator ++x, however, is "increment x, and evaluate to the new value", and that's the behaviour you want, so that's the code you should write.

 

最终,什么计算机指令序列实际产生此行为完全取决于编译器,这可能令人惊讶.只要程序的结果与标准中指定的一样,您就不必担心.

Ultimately, what sequence of computer instructions actually produces this behaviour is completely up to the compiler, and can be surprising. You shouldn't worry about it, as long as the program's result is as specified in the standard.

所以就忘了这个将首先执行"的东西吧;这是垃圾.

So just forget about this "will be executed first" stuff; it's rubbish.

这篇关于为什么在这种情况下括号无法更改c ++运算符的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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