Is assignment operator in c++ returns rvalue or lvalue? And if it is lvalue, which of the two arguments will be incremented here?
(a = b)++
The assignment operators in C and C++ return the value of the variable being assigned to, i.e., their left operand. In your example of a = b , the value of this entire expression is the value that is assigned to a (which is the value of b converted into the type of a ).
The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value.
The value 39 is pulled or fetched (Rvalue) and stored into the variable named age (Lvalue); destroying the value previously stored in that variable. If the expression has a variable or named constant on the right side of the assignment operator, it would pull or fetch the value stored in the variable or constant.
For example, An assignment expects an lvalue as its left operand, so the following is valid: int i = 10; But this is not: int i; 10 = i; This is because i has an address in memory and is a lvalue. While 10 doesn't have an identifiable memory location and hence is an rvalue.
It returns a lvalue. Per § 5.17:
The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.
If those objects have an user-defined operator for assignment, then it depends on implementation and declaration (return type) of the operator=
.
So normally, after
(a = b)++
The object a
will be incremented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With