Reputation: 15337
In particular, the prefix operators' return-by-reference makes sense to me - it's useful, in case one wants to do further operations on the object.
However, I can't get my head around why the postfix operator was designed to return by value.
Is it solely a convention, or was there a good reason why it was designed this way (like a return-by-value does not make sense for postfix, but makes sense for prefix)?
Can someone explain?
ANSWER
Thanks to the answers below, it appears that the postfix operator doesn't necessarily have to return by value (according to the standards).
However, due to the semantic requirements of the postfix operator (return the original value, but increment the reference to the original value afterwards), in conjunction with the standard requirement of:
Operator overloads are functions, and thus all side effects must take place before the function completes.
as explained clearly by David Rodriguez below, bifurcating the value seems to be a necessary consequence of the semantic requirements.
In this context, since we are returning the other value (not the original reference, since it will have changed by the closing brace of the function), returning the other value by-value seems to make the most sense.
Upvotes: 7
Views: 2529
Reputation: 208436
Postfix operators are expressions that yield the original value and then modify the object. At the same time, operator overloads are functions, and thus all side effects must take place before the function completes. The only way of attaining the required semantics is by copying the initial state of the object before applying the change. The original state must be returned by value (if a reference was returned, the evaluation of the expression would yield the state of the object after the function completes, and thus would have the semantics of prefix operators, not postfix ones)
Upvotes: 15
Reputation: 800
Sure it was good reason for that. Post increment operator does following things:
There is no way of returning the reference to the 'old value' of variable. It's gone.
Upvotes: 3
Reputation: 474086
The following code is well-defined in C and C++:
int i = 7;
printf("%i\n", i++ + 2);
This will print 9 to the console, while i
will be 8. Guaranteed.
The postfix increment/decrements modify i
, but they return the old value of i
. The only way to do that with an object is to save the current value in a new value, increment yourself, and return the saved value.
Upvotes: 3
Reputation: 206586
It is the convention because a typical implementation of post fix involves creating a temporary object local to the function, while incrementing the originally passed object using the prefix operator and then returning the temporary object. You cannot return a reference to that local object and hence it needs to be returned by value.
You cannot return a reference because the local object is guaranteed to be alive only within the scope of the function and any access to it beyond this scope will result in Undefined Behavior.
Upvotes: 6