Search code examples
c++cprogramming-languagessyntax

Can someone explain this C/C++ syntax?


Can sombody explain how this works?

int x, y;
....
(some_condition ? x : y) = 100;

Is this intended to work or is is just a "blind" translation or the compiler (something like vec[10] equals 10[vec])?


Solution

  • This is valid C++ and invalid C.

    The result of a conditional expression can be (and in this case is) an lvalue in C++ refering to one of x or y depending on whether some_condition evaluates to true. In C++ either x is assigned the value 100 if some_condition is true when converted to a bool, otherwise y is assigned 100.

    In C, the result of a conditional expression is never an lvalue and cannot be assigned to.