Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "=" operator return?

Tags:

c++

From what I understand, C++ works from left to right. For example, if I do:

std::cout << "output" << "stream";

C++ first begins at the leftmost thing (std::cout), next there is the << operator which takes the string literal at the right ("output") and put it to the object at the left of the operator (std::cout). C++ then returns the object at the left of the operator (std::cout) and then continues the code, another << operator.

What does the "=" operator return?

If I do

double wage = 5;
double salary = wage = 9999.99;

I thought the "=" operator would return only the left or right operand of the "=". So, with my logic, in the line of the salary initialization, salary is initialized with the value of wage, then the "=" operator returns either salary or wage (let's say salary), then it assign 9999.99 to salary, but wage is gone, it should keep its value of 5.

But when I check the value of "salary" and "wage" after the "salary" initialization, both have a value of 9999.99. If I apply the same logic I used with std::cout above, there should be only one variable, either "salary" or "wage", with a value of 9999.99

like image 455
gigi Avatar asked Jan 02 '17 18:01

gigi


2 Answers

The assignment operator is right to left associative, and generally speaking returns its left operand by reference. Generally speaking meaning, this is true for all built in types, library types that I can think of, and that's how you are expected to write assignment operators.

That means that

double salary = wage = 9999.99;

Is exactly the same as

wage = 9999.99;
double salary = wage;

Note that in the second line here, salary gets set to wage, not 9999.99. The distinction doesn't matter here but in some cases it may. For instance (thanks to Justin Time for example):

double salary = --(wage = 9999.99);

salary clearly gets a value of 9998.99 regardless, but its important to note that wage does as well; if assignment returned the right argument instead then wage would still end up being 9999.99 because the temporary would get decremented instead after the assignment to wage had occurred.

As Ben Voigt points out in the comments below, while my answer is correct, the example from the question that I use is slightly misleading. This is because despite the = token showing up twice in the code in question, double salary = wage = 9999.99 actually is not invoking assignment twice, but rather invoking assignment and then constructing a new double. The example would be better continued as follows:

double salary = 0.0;
salary = wage = 9999.99;

Now we are genuinely chaining assignment operators, and all my previous comments about precedence and returns are applicable to the second line of code here.

like image 107
Nir Friedman Avatar answered Oct 01 '22 05:10

Nir Friedman


Consult the operator precedence table. The assignment operator, =, is "right to left" associativity. So the expression is effectively evaluated as:

double salary = (wage = 9999.99);

With wage = 9999.99 occurring first.

like image 40
selbie Avatar answered Oct 01 '22 07:10

selbie