Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is assignment operator in c++ returns rvalue or lvalue?

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)++
like image 854
Sukhanov Niсkolay Avatar asked Oct 08 '13 09:10

Sukhanov Niсkolay


People also ask

What does the assignment operator return in C?

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 ).

What is the return value of assignment operator?

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.

How are lvalue and rvalue used around assignment operators?

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.

What is lvalue and rvalue in C with example?

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.


1 Answers

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.

like image 126
masoud Avatar answered Sep 28 '22 08:09

masoud