C# 编译器行为问题?

发布于 2024-08-07 10:23:12 字数 196 浏览 6 评论 0原文

大家好,下面的代码中,第二个表达式之后 d 的结果应该是什么?

        int d = 1;
        d += d++;

之后人们会假设 d 为 3,但一元增量 d++ 似乎没有生效,并且 d 保留值 2。

这个 bug 有名称吗?是否存在其他支持一元增量(如 C#)的编译器?

Hey everyone, in the following code, what should the result of d be after the second expression?

        int d = 1;
        d += d++;

One would assume d is 3 afterwards but the unary increment d++ doesn't seem to take effect and d retains a value of 2.

Is there a name for this bug? Does it exist for other compilers that support unary increment like C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一向肩并 2024-08-14 10:23:12

这不是一个错误,它的行为完全符合预期。

+= 运算符扩展为:

d = d + d++;

这意味着当结果分配回变量时,++ 运算符引起的更改将被覆盖。

It's not a bug, it acts exactly as expected.

The += operator expands into this:

d = d + d++;

That means that the change that the ++ operator causes is overwritten when the result is assigned back to the variable.

掩饰不了的爱 2024-08-14 10:23:12

如果你看一下生成的 IL,你就会明白为什么结果是 2 而不是 3。

IL_0000:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0001:  stloc.0   // pop and store value in local 0
IL_0002:  ldloc.0   // load value of local 0 on evaluation stack
IL_0003:  ldloc.0   // repeat, stack is now 1, 1
IL_0004:  dup       // duplicate topmost value on evaluation stack, 
                    // i.e. stack is now 1, 1, 1
IL_0005:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0006:  add       // add two topmost values on stack, 
                    // i.e. 1 and 1 and push result on stack
IL_0007:  stloc.0   // pop and store this value in local 0
IL_0008:  add       // add the two remaining values on the stack
                    // which again happens to be 1 and 1 and push result to stack
IL_0009:  stloc.0   // pop and store this value in local 0

换句话说:最终存储的值是 1 和 1 的和。

(上面的代码来自发布模式构建)

If you take a look at the generated IL, you'll see why the result is 2 and not 3.

IL_0000:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0001:  stloc.0   // pop and store value in local 0
IL_0002:  ldloc.0   // load value of local 0 on evaluation stack
IL_0003:  ldloc.0   // repeat, stack is now 1, 1
IL_0004:  dup       // duplicate topmost value on evaluation stack, 
                    // i.e. stack is now 1, 1, 1
IL_0005:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0006:  add       // add two topmost values on stack, 
                    // i.e. 1 and 1 and push result on stack
IL_0007:  stloc.0   // pop and store this value in local 0
IL_0008:  add       // add the two remaining values on the stack
                    // which again happens to be 1 and 1 and push result to stack
IL_0009:  stloc.0   // pop and store this value in local 0

In other words: The final value stored is the sum of 1 and 1.

(the code above is from release mode build)

恏ㄋ傷疤忘ㄋ疼 2024-08-14 10:23:12

如果您以这种方式重写代码,它将把 d 设置为 3:

int d = 1;
d += ++d;

查看 ++ Operator 文档,以解释为什么您的示例的行为方式如此。
摘抄:

第二种形式是后缀增量
手术。操作结果
是其之前的操作数的值
已增加。

正如 @Guffa 指出的,这不是一个错误,只是 d 中后缀增量操作的结果被 += 操作覆盖。

If you rewrite your code this way, it will set d to have a value of 3:

int d = 1;
d += ++d;

Take a look at the ++ Operator documentation for an explanation as to why your example behaves the way it does.
Excerpt:

The second form is a postfix increment
operation. The result of the operation
is the value of the operand before it
has been incremented.

As @Guffa pointed out, it's not a bug, it's simply that the result of your postfix increment operation in d is being overwritten by the += operation.

π浅易 2024-08-14 10:23:12

我经常收到有关 ++ 运算符“损坏”的问题;几乎总是因为提出问题的人习惯了某些行为 ++ 没有明确定义的语言(例如 C++)中的工作方式。这是我最近写的关于这种情况的文章:

http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx

I frequently get questions about the ++ operators being "broken"; almost always it is because the person asking the question is used to the way it works in some language where behaviour ++ is not well-defined, like C++. Here's a recent article I wrote about such a scenario:

http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx

风铃鹿 2024-08-14 10:23:12

你尝试过++d吗? d++不是之后评估的吗?

Have you tried ++d? Isn't d++ evaluated after?

神经暖 2024-08-14 10:23:12

我认为 d++ 的行为与 ++d 不同,尽管存储在 d 中的最终结果是相同的。

I think behavior of d++ is different than ++d, though the final result stored in d is the same.

旧梦荧光笔 2024-08-14 10:23:12

愚蠢的代码是不可预测的。我可以推荐一下吗

d += 2;

Silly code is unpredictable. Might I recommend

d += 2;
落叶缤纷 2024-08-14 10:23:12

d++ 和 ++d 是不同的。也称为“选择未损坏。

d++ and ++d are different. Also known as "Select Isn't Broken."

始于初秋 2024-08-14 10:23:12

...这就是为什么我发现后/前递增/递减运算符在与其他运算符一起用于表达式时非常不可读的原因的一个例子。您描述的行为是正确的,但很难推理,导致误解和错误。

尽管它比较冗长,但我会将其重写为:

int d = 1;
d += d;
++d;

请注意使用前增量运算符而不是后增量运算符,以便编译器认为它不需要保留旧值的副本。

...and this would be an example of the reason why I find the post/pre- increment/decrement operators to be highly unreadable when used in an expression with other operators. The behavior you describe is correct, but hard to reason about, leading to misunderstandings and bugs.

Even though it's wordier I would rewrite this as:

int d = 1;
d += d;
++d;

Note the use of the pre-increment operator instead of the post-increment so that the compiler doesn't think it needs to keep a copy of the old value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文