Why are postfix statements designed to return by value?

In particular, the correct-correction of prefix operators makes sense to me - it is useful if you want to do further operations on the object.

However, I cannot understand why the postfix operator was created to return by value.

Is this the only convention or is there a good reason why it was designed in this way (for example, the return value does not make sense for the postfix, but makes sense for the prefix)?

Can someone explain?

ANSWER

Thanks to the answers below, it seems that the postfix operator does not have to return by value (in accordance with standards).

However, due to the semantic requirements of the postfix operator (return the original value, and then increase the link to the original value later) in combination with the standard requirement:

Operator overloads are functions, so all side effects must be performed before the function completes.

as David Rodriguez clearly explains below, the split of meaning seems to be a necessary consequence of semantic demands.

In this context, since we are returning a different value (and not the original link, since it will be changed by the closing bracket of the function), returning another value by value seems to make the most sense.

+4
source share
4 answers

Postfix statements are expressions that give the original value and then modify the object. At the same time, operator overloads are functions, and therefore all side effects must take place before the function ends. The only way to get the required semantics is to copy the initial state of the object before applying the change. The initial state should be returned by the value (if the link was returned, evaluating the expression would give the state of the object after the function was completed and, therefore, would have the semantics of prefix operators, not postfix ones)

+9
source

This is an agreement, since a typical post fix implementation involves creating a temporary object local to this function, while incrementing the original object using the prefix operator and then returning the temporary object. You cannot return a link to this local object, and therefore it must be returned by value.

You cannot return the link because the local object will be available only within the function, and any access to it outside this area will lead to Undefined Behavior.

+5
source

The following code is well defined in C and C ++:

int i = 7; printf("%i\n", i++ + 2); 

This will display 9 on the console, and i will be 8. Guaranteed.

The increment / Decments postfix changes i , but returns the old value of i . The only way to do this with the object is to save the current value in the new value, grow itself and return the saved value.

+3
source

Of course, that was a good reason for this. The post increment statement does the following:

  • increases the value of the variable and
  • returns its old value.

There is no way to return a reference to the "old value" of a variable. He left.

+3
source

All Articles