Why does assignment operator return lvalue?


T

Tapeesh

In C++, why does assignment operator always return a lvalue. Even in
cases of assignment of basic datatypes like int ?

For eg.

int main()
{
int a, b, c;
a = b + c;
return 0;
}

Here, in the above example the assigment returns an lvalue in C++,
whereas in C it would be a rvalue.
Is it a C++ standard ? If yes, then what is the logic behind doing such
a thing????
 
Ad

Advertisements

J

John Carson

Tapeesh said:
In C++, why does assignment operator always return a lvalue. Even in
cases of assignment of basic datatypes like int ?

For eg.

int main()
{
int a, b, c;
a = b + c;
return 0;
}

Here, in the above example the assigment returns an lvalue in C++,
whereas in C it would be a rvalue.
Is it a C++ standard ?

Yes. Section 5.17/2 of the C++ Standard says:

"There are several assignment operators, all of which group right-to-left.
All require a modifiable lvalue as their left operand, and the type of an
assignment expression is that of its left operand. The result of the
assignment operation is the value stored in the left operand after the
assignment has taken place; the result is an lvalue."
If yes, then what is the logic behind doing such a thing????

See here, especially the comment of Andrew Koenig

http://www.codecomments.com/archive323-2005-4-445127.html
 

Top