简体   繁体   中英

Low level details of C/C++ assignment operator implementation. What does it return?

I ma total newbie to a C++ world (and C too). And don't know all its details. But one thing really bothers me. It is constructions like : while (a=b) {...} .As I understand this magic works because assignment operator in C and C++ returns something. So the questions: what does it return? Is this a documented thing?Does it work the same in C and C++. Low level details about assignment operator and its implementation in both C and C++ (if there is a difference) will be very appreciated!

I hope that this question won't be closed, because I can't find a comprehensive explanation and good material on this theme from the low level point of view all the more so.

For built-in types in C++ evaluating an assignment expression produces an lvalue that is the left hand side of the assignment expression. The assignment is sequenced before the result can be used, so when the result is converted to an rvalue you get the newly assigned value:

int a, b=5;
int &c = (a=b);
assert(&c==&a);
b=10;
assert(10==(a=b));

C is almost but not exactly the same. The result of an assignment expression in C is an rvalue the same as the value newly assigned to the left hand side of the assignment.

int *c = &(a=b); // not legal in C because you can only take the address of lvalues.

Usually if the result of an assignment is used at all it's used as an rvalue (eg, a=b=c ), so this difference between C++ and C largely goes unnoticed.

The assignment operator is defined (in C) as returning the value of the variable that was assigned to - ie the value of the expression (a=b) is the value of a after the expression has been evaluated.

It can be defined to be something different (of the same type) for user-defined operator overloads in C++, but I suspect most would consider this to be a very unpleasant use of operator overloading.

You can use this (non-boolean) value in a while (or an if , etc.) because of type conversion - using a value in a conditional context causes it to be implicitly converted to something which makes sense in a conditional context. In C++, this is bool , and you can define your own conversion (for your own type) by overloading operator bool() . In C, anything other than 0 is true.

To understand such expressions, you have to first understand that, positive integers are considered as 'true' and 0 is considered as false.

An assignment evaluates to the left hand side of the operator = as its value. So, while(a=b) { } would mean, while(1 /*true*/) if a after being assigned to b evaluates to non-zero. Else, it is considered as while(0 /*false*/)

Similarly, with the operator (a=b)?1:0 is the value of a after being assigned to b .. if it is non-zero then the value is taken as true and the statement following ? will be executed, or the statement following : is executed.

Assignments usually evaluate to the value at the left hand side of the operator = where as, logical operators(such as == , && etc) evaluate to 1 or 0.

Note: with C++, it will depend upon if or not, a certain operator is overloaded.. and it will also depend upon the return type of the overloaded operator.

The assignment operators in C and C++ return the value of the variable being assigned to, ie, 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 ).

So you can say that the assignment operator "returns" the value of its left operand.

In C++ it's a little more complicated because you can overload the = operator with an actual user-defined function, and have it return something other than the value (and type) of the left operand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM