前缀增量/减量的优先级低于数组下标 [英] Prefix increment/decrement's Precedence is lower than Array subscripting

查看:101
本文介绍了前缀增量/减量的优先级低于数组下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前缀增量/减量的优先级低于数组下标?

我从引用它这里 [ ^ ]



我很困惑,如何解释这个表达式?

Prefix increment/decrement's Precedence is lower than Array subscripting?
I referenced it from here[^]

I'm puzzled, how to explain this expression?

int buff[5] = { 10, 20, 30, 40, 50 };
int idx = 0;
int num = buff[++idx];
assert(num == buff[1]);



结果显示,+ +先执行然后再执行[]。但它与我引用的优先表相冲突。



谁都知道?非常感谢。


as the result shows, ++ excute first then []. but it conflict with the precedence table as I referenced.

anyone knows? thanks very much.

推荐答案

优先级表只在表达式中查阅,它对表达式没有影响。在使用结果之前完全评估表达式。



数组去引用是一个复合表达式。它由两个组件表达式组成,一个在方括号外,一个在里面:



base_address_expression [index_expression]



index_expression在用作整体表达式的一部分之前完全评估:



const auto t = index_expression;

base_address_expression [t]



无论index_expression是什么,都会发生这种情况 - 函数调用,另一个数组解除引用或其他复杂的算术表达式。首先完全计算,然后使用。
The precedence table is only consulted within an expression, it has no effect across expressions. Expressions are completely evaluated before their results are used.

An array de-reference is a composite expression. It consists of two component expressions, one outside the square brackets and one inside:

base_address_expression[ index_expression ]

index_expression is completely evaluated before it's used as part of the overall expression:

const auto t = index_expression;
base_address_expression[ t ]

This happens whatever index_expression is - a function call, another array dereference or another complex arithmetic expression. It get's calculated first, completely, then used.


这不是优先级的情况,而是评估函数arguments.operator []是一个带有一个参数的函数,这个参数必须始终在将其传递给函数之前进行评估。因为在这种情况下,参数是表达式,所以表达式的结果用作参数。如果你想传递0而不是1,你可以使用后缀增量。



只有在你使用增量运算符时才会应用运算符优先级规则code> buff 像这样:

This is not a case of precedence, but of evaluating function arguments.operator[] is a function with one argument, and this argument must always be evaluated before passing it to the function. Since in this case the argument is an expression, the result of the expression is used as an argument. You could use the postfix increment instead if you wanted to pass 0, rather than 1.

The operator precedence rules would only be applied if you used the increment operator on buff like this:
int num = ++buff[idx];





PS - 对我的第一个陈述的解释:

在C ++中,您可以为处理特定类的参数的运算符重载定义。这称为运算符重载。有很多关于这个主题的文章,例如此处 [ ^ ],但我指的是当你重载[]运算符时,函数的签名必须看起来像一个函数的签名argument:要访问的元素的索引值。



这意味着无论何时调用[]运算符,它都被内部视为对函数的调用,然后做的第一件事就是评估它的参数,索引值。



P.S. - an explanation of my first statement:
In C++ you can overload definitions for operators working on arguments of a specific class. This is called operator overloading. There are plenty of articles on this topic, e.g. here[^], but the one thing I was referring to is that when you overload the [] operator, the signature of the function must look like that of a function with one argument: the index value of the element you want to access.

That means whenever you invoke the [] operator, it is internally treated like a call to a function, and the first thing that is done then is that it's argument, the index value, is evaluated.


这篇关于前缀增量/减量的优先级低于数组下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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