后缀(preFIX)增量,L值和R值(以C和C ++) [英] postfix (prefix) increment, L-value and R-value (in C and C++)

查看:134
本文介绍了后缀(preFIX)增量,L值和R值(以C和C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只学会了以下事实:


      
  • 一preFIX递增(++ VAR_NAME)的结果是C(至少我是一个R值
        确定它是不是在C L值),但它是在C ++中的L值

  • The result of a prefix increment (++var_name) is an R-value in C (at least, I am sure that it is not a L-value in C), but it is an L-value in C++.

后缀增量(VAR_NAME ++)的结果是C中的R值(至少我是
    确定它是不是在C L值)。这也是用C ++真(它说的结果
    是prvalue)。

The result of a postfix increment (var_name++) is an R-value in C (at least, I am sure that it is not a L-value in C). This is also true in C++ (It says the result is a prvalue).

我VS2010(的.cpp和.C)和Ubuntu(gcc和g ++),检查了这些。

I checked these in VS2010 (.cpp and .c) and Ubuntu (gcc and g++).

在C ++标准的 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf ,它是写

In p.109 (5.3.2) of C++ Standard http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, it is written

preFIX ++的操作数加1修改,或者设置为true,如果它是布尔(这是使用德precated)。该
  操作应修改的左值。类型的操作数须为算术类型或指向
  一个完全定义的对象类型。 结果是更新操作;它是一个左值,然后...

The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The result is the updated operand; it is an lvalue, and...

和在第101页,(5.2.6)

and in p.101, (5.2.6)

后缀++前pression的价值是其操作数的值。 ... 结果是
  prvalue
。类型的结果的是操作数的类型的CV-不合格版本。另见5.7和5.17。

The value of a postfix ++ expression is the value of its operand. ... The result is a prvalue. The type of the result is the cv-unqualified version of the type of the operand. See also 5.7 and 5.17.

(不知R值和prvalue之间的差虽然)。

(I don't know the difference between R-value and prvalue though).

至于C标准 HTTP:// WWW。 open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
preFIX ++ 6.5.3.1中的描述和后缀是6.5.2.4,但是从描述,我不能得到一个明确的,明确的答复。

As to C standard http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf, prefix ++ is described in 6.5.3.1 and postfix is in 6.5.2.4, but from the description, I can't get a clear, definite answer.

我想知道,让他们成为R值或L值的原因。我所知道的是,

I would like to know the reasons that make them being R-value or L-value. All I know is that

我们可以一个值赋给一个(修改)L-值,例如,一个变量名。 R值是前pression的值。

但是我不知道的细节,为什么后缀++是不是在C和C ++一个L值,为什么preFIX ++是不是在C (我看到的东西像后缀++ ... ...存储在一个临时地址,那么......,但我还是不明白这一点)。

But I don't know the details why postfix ++ is not a L-value in C and C++, and why prefix ++ is not in C. (I saw something like "postfix ++...store...in a temporary address, then...", but I still don't get it).

和另外一个问题是,为什么preFIX ++是在C和C ++有什么不同?制作preFIX ++中的L值(在C ++中)有许多优点?如果是这样,为什么C没有改变呢? (其他原因比向后兼容性,或至少是为什么改变会造成很多问题)。

And another question is that why prefix ++ is different in C and C++? Making prefix ++ a L-value (in C++) has many advantages? If so, why C doesn't change this? (Other reasons than backward compatibility, or at least why changing it will cause a lot of problems).

推荐答案

C和C ++是不同的语言。 C ++有运算符重载和C没有。在 ++ 运营商,无论是preFIX或后缀,是可以在C重载运算符++。 C ++也有引用和C没有。

C and C++ are different languages. C++ has operator overloading and C does not. The ++ operators, whether prefix or postfix, are operators which can be overloaded in C++. C++ also has references and C does not.

在C, ++我我++ 都产生一个值,它不是一个的左值的。这是需要的,否则你可以用相抵触未定义行为试图修改同一个序列点范围内同标运行。

In C, ++i and i++ both yield a value which is not an lvalue. This is desirable, as otherwise you could run afoul with undefined behaviour by trying to modify the same scalar within the same sequence-point boundaries.

深思:在C语言中,逗号操作符还生产值不是一个左值,所以滴的 lvalueness 的,你可以这样做:

Food for thought: In C, the comma operator also produces a value which is not an lvalue, so to "drop" lvalueness, you can do:

(0, lvalue)

这篇关于后缀(preFIX)增量,L值和R值(以C和C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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