4

I know that k-- equals to k = k - 1. And I am wondering that what happens to k = k-- which I think may cause infinite loop. But in fact, I compile this code in Visual Studio 2017 and the output of k = k-- is the same as k = k - 1.

So what's the implication of k = k--?

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Boooooooooms
  • 306
  • 4
  • 21
  • Which C++ Standard are you targeting? Never mind VS 2017 targets C++14, yes? – user4581301 Nov 30 '18 at 05:04
  • Handy reading on the topic: [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) Warning: It goes on for a while. – user4581301 Nov 30 '18 at 05:10
  • GCC produce `eax = k; edx = eax - 1; k = edx; k = eax;`, which remains `k` unchanged. – Geno Chen Nov 30 '18 at 05:10
  • 1
    Anyway, the kicker is as of C++17 this should be defined behaviour. – user4581301 Nov 30 '18 at 05:11
  • @user4581301 Ja, I'm already waiting for the new wave of confusion that will bring us. Btw sequece-points are gone since C++11. – Swordfish Nov 30 '18 at 05:22
  • 1
    Why would we want to "use `k = k--`" in the first place ? – Sid S Nov 30 '18 at 05:24
  • @SidS It looks fancy. – Swordfish Nov 30 '18 at 05:29
  • 1
    You wouldn't. But as a Language Lawyer question it's a valid exploration of the language. I'm just waffling over whether or not I should dupe hammer it shut with [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – user4581301 Nov 30 '18 at 05:29
  • 2
    @user4581301 Better not. This is a very specific question which already has a good answer by Jans. – Swordfish Nov 30 '18 at 05:31
  • 1
    Even before Jans' answer I was angling that way. I'd much rather a short, specific answer than an answer buried in pages of general answers. – user4581301 Nov 30 '18 at 05:33
  • Looking through the [MSVS support table](https://learn.microsoft.com/en-us/cpp/visual-cpp-language-conformance?view=vs-2017), C++17 support looks pretty good in VS 2017 if your patching is up to date. – user4581301 Nov 30 '18 at 05:37
  • If you don't understand what it does, don't write it in production code. When you have to debug, you'll have to find out again. Use some extra characters to make it clear for everyone, including you in 6 months – JVApen Nov 30 '18 at 07:40

2 Answers2

9

Before the behavior was undefined, after that, It's fine according to [expr.ass]/1:

[...] the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand

and sequenced before means according to [intro.execution]/8

[...] An expression X is said to be sequenced before an expression Y if every value computation and every side effect associated with the expression X is sequenced before every value computation and every side effect associated with the expression Y.

In conclusion, k=k-1 and k=k-- are guaranteed to yield the same result.

also see: cppreference

Jans
  • 11,064
  • 3
  • 37
  • 45
4

In C and C++ before C++17 it is undefined behavior. The fact that it gave a reasonable answer on your compiler is no guarantee that it will do so on other compilers, or even in other contexts with the current compiler.

AShelly
  • 34,686
  • 15
  • 91
  • 152