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????
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top