在参数列表中重用变量时的前缀增量变量 [英] Prefix increment variable when variable is reused in parameter list

查看:53
本文介绍了在参数列表中重用变量时的前缀增量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天面临一个特殊的问题.

I'm facing a peculiar question today..

当我在函数参数列表中的变量上使用前缀增量,然后又在参数列表中再次使用该变量时,会发生什么?前缀增量会在变量作为第二个参数的一部分求值之前发生吗?

What happens when I use the prefix increment on a variable inside a function parameter list, but then reuse that variable again in the parameter list? Will the prefix increment happen before the variable gets evaluated as part of the second parameter?

(我想我也可能会问,如果要改用后缀增量怎么办?)

(And I suppose I might as well ask, what if one were to use a postfix increment instead?)

Example(++marker1, marker2 - marker1);

推荐答案

对函数参数的求值是无序列的,因此在一个变量中修改变量并在另一个变量中使用其值将产生不确定的行为.不论是使用前增量还是后增量,都是这种情况.

The evaluation of the function arguments is unsequenced, so modifying a variable in one and using its value in another gives undefined behaviour. That's the case whether you use pre-increment or post-increment.

通常,避免将变量作为复杂表达式的一部分进行修改;尤其是如果在表达式中的其他地方使用了它的值,因为它往往会给出未指定的值或(在某些情况下)未定义的行为.

In general, avoid modifying a variable as part of a complicated expression; especially if its value is used elsewhere in the expression, since that tends to give an unspecified value or (in some cases) undefined behaviour.

这篇关于在参数列表中重用变量时的前缀增量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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