Get the latest tech news

Is `(expession, lvalue) = rvalue` a valid assignment in C or C++?


Some time ago I stumbled upon the idea of how a C construct, such as (expr0, expr1, expr2), evaluates (see "What does the comma operator , do?" for more context). I've started experimenting

I've started experimenting with this, especially inside of function-like macros, and recently found a code which is rejected by some compilers, while accepted by others. At first, I thought it's a bug within tcc, since it's the only C compiler which does not reject the "faulty" code, but then I checked C++ front-ends and I'm not so sure about it any more. In your example, arr[0] is an operand of the, operator, so should lose its lvalue-ness; so tcc is wrong, and the other C compilers are correct.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of expession

expession

Photo of valid assignment

valid assignment

Related news:

News photo

Python, C++ inspired language that transpiles to C and can be embedded within C

News photo

Contracts for C++ (DbC) [pdf]

News photo

The US government wants developers to stop using C and C++