关于++运算符的C和C ++的区别 [英] The difference between C and C++ regarding the ++ operator

查看:121
本文介绍了关于++运算符的C和C ++的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我已经在使用一些代码愚弄了一些我不明白的为什么 int i = 6;
int j;

int * ptr =& i;
int * ptr1 =& j

j = i ++;

//现在j == 6和i == 7.直截了当。

如果将运算符放在等号的左边会怎么样?

  ++ ptr = ptr1; 

相当于

 (ptr = ptr + 1)= ptr1; 

  ptr ++ = ptr1; 

相当于

  ptr = ptr + 1 = ptr1; 

后缀运行编译错误,我得到它。在赋值运算符的左侧有一个常数ptr + 1。很公平。



前缀一个在C ++中编译和WORKS。是的,我明白它是凌乱的,你在处理未分配的内存,但它的工作和编译。在C中这不会编译,返回与后缀作为左操作数赋值所需的左值相同的错误。这种情况发生,无论它是如何编写,扩展与两个=运算符或与++ ptr语法。



C处理之间的区别是什么在C和C ++中, x ++

在C和C ++中, / code>是一个右值,因此不能赋值。



在C中, ++ x 等价于 x + = 1 (C标准§6.5.3.1/ p2;所有C标准引用均来自WG14 N1570)。在C ++中, ++ x 等效于 x + = 1 if x 不是 bool (C ++标准§5.3.2[expr.pre.incr] / p1;所有C ++标准引用的是WG21 N3936) p>

在C中,赋值表达式的结果是一个右值(C标准§6.5.16/ p3):



< >

赋值运算符在由
左操作数指定的对象中存储一个值。赋值表达式在赋值后具有左
操作数的值,但不是一个左值。


一个左值,你不能分配给它:(C标准§6.5.16/ p2 - 注意这是一个约束)


赋值运算符应具有可修改的左值作为它的左
操作数。


在C ++中,赋值表达式的结果是一个左值(C ++标准§5.17[expr.ass] / p1):


赋值运算符
组从右到左。所有都需要一个可修改的左值作为它们的左
操作数,并返回一个指向左操作数的左值。


$ c> ++ ptr = ptr1; 是C中可诊断的约束违反,但不违反C ++中的任何可诊断规则。



,pre-C ++ 11, ++ ptr = ptr1; 有未定义的行为,因为它修改 ptr 两个相邻的序列点。



在C ++ 11中, ++ ptr = ptr1 的行为。如果我们将它重写为

则更清楚。

 (ptr + = 1)= ptr1; 

从C ++ 11开始,C ++标准规定(§5.17[expr.ass] / p1 )


在所有情况下,赋值在右和左操作数的值计算
之后,计算
赋值表达式。对于
不确定顺序的函数调用,复合
赋值的操作是单个评估。


因此 ptr + = 1 和<$ c的值计算之后, = $ c> ptr1 。由 + = 执行的赋值在 ptr + = 1 的值计算之前排序,由 + = 必须在该赋值之前排序。因此,这里的顺序是明确的,没有未定义的行为。


I have been fooling around with some code and saw something that I don't understand the "why" of.

int i = 6;
int j;

int *ptr = &i;
int *ptr1 = &j

j = i++;

//now j == 6 and i == 7. Straightforward.

What if you put the operator on the left side of the equals sign?

++ptr = ptr1;

is equivalent to

(ptr = ptr + 1) = ptr1; 

whereas

ptr++ = ptr1;

is equivalent to

ptr = ptr + 1 = ptr1;

The postfix runs a compilation error and I get it. You've got a constant "ptr + 1" on the left side of an assignment operator. Fair enough.

The prefix one compiles and WORKS in C++. Yes, I understand it's messy and you're dealing with unallocated memory, but it works and compiles. In C this does not compile, returning the same error as the postfix "lvalue required as left operand of assignment". This happens no matter how it's written, expanded out with two "=" operators or with the "++ptr" syntax.

What is the difference between how C handles such an assignment and how C++ handles it?

解决方案

In both C and C++, the result of x++ is an rvalue, so you can't assign to it.

In C, ++x is equivalent to x += 1 (C standard §6.5.3.1/p2; all C standard cites are to WG14 N1570). In C++, ++x is equivalent to x += 1 if x is not a bool (C++ standard §5.3.2 [expr.pre.incr]/p1; all C++ standard cites are to WG21 N3936).

In C, the result of an assignment expression is an rvalue (C standard §6.5.16/p3):

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

Because it's not an lvalue, you can't assign to it: (C standard §6.5.16/p2 - note that this is a constraint)

An assignment operator shall have a modifiable lvalue as its left operand.

In C++, the result of an assignment expression is an lvalue (C++ standard §5.17 [expr.ass]/p1):

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.

So ++ptr = ptr1; is a diagnosable constraint violation in C, but does not violate any diagnosable rule in C++.

However, pre-C++11, ++ptr = ptr1; has undefined behavior, as it modifies ptr twice between two adjacent sequence points.

In C++11, the behavior of ++ptr = ptr1 becomes well defined. It's clearer if we rewrite it as

(ptr += 1) = ptr1;

Since C++11, the C++ standard provides that (§5.17 [expr.ass]/p1)

In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.

So the assignment performed by the = is sequenced after the value computation of ptr += 1 and ptr1. The assignment performed by the += is sequenced before the value computation of ptr += 1, and all value computations required by the += are necessarily sequenced before that assignment. Thus, the sequencing here is well-defined and there is no undefined behavior.

这篇关于关于++运算符的C和C ++的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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