1

Reading through the Bjarne's CPP book I've encountered such a statement: "Where logically feasible, the result of an operator that takes an lvalue operand is an lvalue denoting that lvalue operand", and I really can't wrap my head around it. These are the examples included with this statement:

void f(int x, int y)
{
    int j = x = y; // the value of x=y is the value of x after the assignment
    int∗ p = &++x; // p points to x
    int∗ q = &(x++); // error : x++ is not an lvalue (it is not the value stored in x)
    int∗ p2 = &(x>y?x:y); // address of the int with the larger value
    int& r = (x<y)?x:1; // error : 1 is not an lvalue
}

Code itself makes sense to me, but it makes sense from my personal understanding of how these operators work. But I can't really apply the statement here, for example for the first line. OK, = is an operator which can take both lvalue and rvalue operands (and from my understanding lvalue is implicitly converted to rvalue in this case) , then the result of x = y is an lvalue denoting y? If I follow this correctly, then I could write int* j = x = y, but this would be a compile-time error, as the result of x = y is clearly an rvalue. So I am really confused here.

Can anyone clarify what is this statement is semantically about and how it relates to the given examples step by step?

nyarian
  • 4,085
  • 1
  • 19
  • 51
  • 2
    The result of `x = y` is an lvaule denoting `x` (which contains the value from `y`) – Richard Critten Aug 02 '20 at 14:30
  • 1
    `x = y` is an integer value, you can't assign it to a pointer, but `int * i = &(x = y)` works – Alexey S. Larionov Aug 02 '20 at 14:30
  • 1
    @AlexLarionov if the result of the assignment is an integer then the pointer assignment would not work as that requires an lvaule. – Richard Critten Aug 02 '20 at 14:32
  • @RichardCritten well I understand now my mistake in wording, thanks – Alexey S. Larionov Aug 02 '20 at 14:36
  • @RichardCritten if it would be an lvalue then it would be possible to write `int* a = b = c;` but it is not, right? – nyarian Aug 02 '20 at 14:48
  • @SaiSreenivas that makes sense because `&` can be viewed here as lvalue obtaining by brute force (there is a shadow variable under the hood that contains the result of `x = y` if I am not mistaken), but it doesn't quite match the statement I faced in the book... – nyarian Aug 02 '20 at 14:51
  • 1
    Well, it turns out it's possible to write `int& j = x = y;`, so it seems that yeah, `=` operator really returns an lvalue denoting the `x` after the assignment was made! And with this the statement itself starts to make sense. Would someone like to post an answer here so I'll make it as correct? – nyarian Aug 02 '20 at 14:52

1 Answers1

0
  • x = y returns lvalue referring to left-hand operand.
  • int* j = x = y, this is erroneous because of invalid types of the operands.

But,

  • int* j = &(x = y) :

    works, as x = y returns l-value so this boils down to int *j = &x (value of x is y)

    and similarly, this int& j = x = y also works

Additional links:

For conditional expressions: Return type of '?:' (ternary conditional operator)

Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16