为什么预增量运算符在C中给出右值? [英] Why pre-increment operator gives rvalue in C?

查看:79
本文介绍了为什么预增量运算符在C中给出右值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,预增量运算符提供左值,因为返回的是增量对象本身,而不是副本。
但是在C中,它给出了rvalue。为什么?

In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why?

推荐答案

C没有引用。在C ++中, ++ i 返回对 i (左值)的引用,而在C中,它返回一个副本(递增)。

C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented).

C99 6.5.3.1/2

C99 6.5.3.1/2


该操作数的值prefix ++运算符递增。 结果是递增后操作数的新值。表达式++ E等于(E + = 1)。

The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++Eis equivalent to (E+=1).

''表达式的值'' < => rvalue

但是出于历史原因,我认为引用不属于C可能是一个可能的原因。

However for historical reasons I think "references not being part of C" could be a possible reason.

这篇关于为什么预增量运算符在C中给出右值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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