Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

a parenthesis expression assigned to single lvalue

Why does this code compile ?

int X = (2,4);

I compiled this line with c++ replit

the second value (4) is assigned to X

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The initializing expression is a praimary expression with the comma operator

int X = (2,4);

Its value is the value of the second operand of the comma operator.

In fact this declaration is equivalent to the declaration

int X = 4;

because the first operand has no side effect.

From the C++17 Standard (8.19 Comma operator)

1 The comma operator groups left-to-right.

expression: assignment-expression
expression , assignment-expression

A pair of expressions separated by a comma is evaluated left-to-right;
the left expression is a discarded-value expression (Clause 8). Every
value computation and side effect associated with the left expression
is sequenced before every value computation and side effect associated
with the right expression. The type and value of the result are the
type and value of the right operand; the result is of the same value
category as its right operand, and is a bit-field if its right operand
is a bit-field. If the right operand is a temporary expression (15.2),
the result is a temporary expression.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading