According to the C++ standard, can I be sure that assignment operators for built-in variables return (the original value)?
Or is this implementation dependent (yet simply have most popular compilers implemented this)?
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 return type is important for chaining operations. Consider the following construction: a = b = c; . This should be equal to a = (b = c) , i.e. c should be assigned into b and b into a .
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 ).
Assignment operators are used to assign values to variables.
Yes, it is guaranteed:
5.17 Assignment and compound assignment operators
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.
This applies to built-in types. With user-defined types it can return anything.
It depends on what you mean by "the original value".
For example:
#include <iostream>
int main() {
int i;
std::cout << (i = 1.9) << "\n";
}
prints 1
. The assignment expression yields the new value of the LHS (namely 1), not the "original value" of the RHS (1.9).
I'm not sure whether that's what you meant to ask about.
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