Why does this code compile ?
int X = (2,4);
I compiled this line with c++ replit
the second value (4) is assigned to X
Why does this code compile ?
int X = (2,4);
I compiled this line with c++ replit
the second value (4) is assigned to X
Psonbre
On
It will compile in c++. The only value that is applied is the last one when you use a parenthesis expression with multiple values inside, however all values will be evaluated which means you can do things like this in very specific cases:
int a, b;
a = (b = 3, 5); // assigns 3 to b and 5 to a
The initializing expression is a praimary expression with the comma operator
Its value is the value of the second operand of the comma operator.
In fact this declaration is equivalent to the declaration
because the first operand has no side effect.
From the C++17 Standard (8.19 Comma operator)