I recently asked a question Is the behavior of return x ++; determined by?
The result was about what I expected, but made me think about a similar situation.
If I wrote
class Foo
{
...
int x;
int& bar() { return x++; }
};
Where bar now returns a reference to int, is this behavior defined? If the answer to the previous question is literally correct, and not just a convenient abstraction of what is happening, it would seem that you will return a link to the stack variable, which will be destroyed as soon as the return is made.
If this is just an abstraction, I would be interested to know what behavior is really guaranteed after the increment.
source
share