2

Why functions that return lvalue references, along with prefix increment/decrement operators are lvalue expressions whereas, functions that return lvalue references along with postfix increment/decrement operators are not lvalue expressions ?

C++ Primer, Lippman et al. 5/e mentions :

Functions that return lvalue references, along with the assignment, subscript, dereference, and prefix increment/decrement operators, are all examples of expressions that return lvalues. We can bind an lvalue reference to the result of any of these expressions.

Functions that return a nonreference type, along with the arithmetic, relational, bitwise, and postfix increment/decrement operators, all yield rvalues. We cannot bind an lvalue reference to these expressions, but we can bind either an lvalue reference to const or an rvalue reference to such expressions.*

YSC
  • 38,212
  • 9
  • 96
  • 149
Onkar N Mahajan
  • 410
  • 3
  • 13
  • 1
    Hello, does this answer your question? https://stackoverflow.com/a/3181240/5470596 – YSC Dec 29 '20 at 08:46
  • @YSC, it does give me some hint, but doesn't directly answer my question. I have to work on that hint and arrive at the answer, which I'm doing now. Thanks for pointing out. and best wishes for new year :-) – Onkar N Mahajan Dec 29 '20 at 09:01
  • What would you expect `1+1 = 3;` to do? – Sneftel Dec 29 '20 at 09:15

2 Answers2

1

I will try to answer this myself with some hint that I got from @YSC. Not sure about the correctness. Community please help to review this answer.

In the case of prefix:

The return value (which is a reference) can be incremented (by using overloaded prefix ++ operator), which itself returns the reference to the same object, which can be assigned, in a sense the object maintains the identity throughout this prefix inc/dec operations and so it yields a lvalue expression.

In the case of postfix:

return value is a reference as before, but now the copy of the object (returned by reference) has to be made because there is a post inc/dec operator.

Situation is similar to the case :

int i; 
i+1 = j

i+1 is not a lvalue expression. i+1 has no identity (memory allocation) so it can't be used as lvalue expression.

Onkar N Mahajan
  • 410
  • 3
  • 13
0

Question

Why functions that return lvalue references, along with prefix increment/decrement operators are lvalue expressions whereas, functions that return lvalue references along with postfix increment/decrement operators are not lvalue expressions ?

Answer

Because the standard says so.

From value catogeries's documentation:

The following expressions are lvalue expressions:

  1. a function call or an overloaded operator expression, whose return type is lvalue reference
  1. ++a and --a, the built-in pre-increment and pre-decrement expressions;

Similarly,

The following expressions are prvalue expressions:

  1. a function call or an overloaded operator expression, whose return type is non-reference
  1. a++ and a--, the built-in post-increment and post-decrement expressions
  1. a + b, a % b, a & b, a << b, and all other built-in arithmetic expressions
Jason
  • 36,170
  • 5
  • 26
  • 60