Search code examples
c++returnpost-increment

Is the behavior of return x++; defined?


If I have for example a class with instance method and variables

class Foo
{

   ...

   int x;
   int bar() { return x++; }
 };

Is the behavior of returning a post-incremented variable defined?


Solution

  • Yes, it's equivalent to:

    int bar()
    {
      int temp = x;
      ++x;
      return temp;
    }