samuelbrody1249
samuelbrody1249

Reputation: 4767

Assignment in ternary operator

For a ternary operator, why does the following compile:

a > b ? a=4 : ' ';

But this one does not:

a > b ? ' ' : b=4;

lvalue required as left operand of assignment

That is, what makes the true case different than the false case for assignment?

Upvotes: 3

Views: 1597

Answers (3)

dbush
dbush

Reputation: 223739

It has to do with the formal definition of the conditional operator. From section 6.5.15 of the C standard:

conditional-expression:
  logical-OR-expression
  logical-OR-expression ? expression : conditional-expression

The second clause of a conditional can be any expression, while the third clause can only be a conditional expression (of which as assignment is not). Put another way, the conditional operator ?: has higher precedence than the assignment operator =.

So this:

a > b ? a=4 : ' '

Is the same as this:

(a > b) ? (a=4) : (' ')

But this:

a > b ? ' ' : b=4;

Is the same same as this:

((a > b) ? (' ') : b)=4;

And the result of the conditional operator cannot be assigned to (i.e. it is not an lvalue) so you get an error.

If you add parenthesis you can get something that compiles:

a > b ? ' ' : (b=4);

Granted, these statements don't look like the best use case for a conditional and should probably be rewritten as:

if (a>b) a=4;

And:

if (a<=b) b=4;

Upvotes: 10

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

It is the case where there is an essential difference between C and C++ that you should know.:)

In C the conditional (ternary) operator is defined like

conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

while in C++ like

conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression

So in C this statement

a > b ? ' ' : b=4;

is equivalent to

( a > b ? ' ' : b ) = 4;

So the compiler issues an error because you may not assign a value to other value.

From the C Standard (6.5.15 Conditional operator)

4 The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below

In C++ this statement is equivalent to

a > b ? ' ' : ( b=4 );

and is a valid statement.

Here is a demonstrative program

#include <iostream>

int main() 
{
    int a = 0, b = 1;
    
    a > b ? ' ' : b=4;
    
    std::cout << "b = " << b << '\n';
    
    return 0;
}

Its output is

b = 4

Upvotes: 2

Eugene Sh.
Eugene Sh.

Reputation: 18299

The :? has higher operator precedence than assignment operator. So the latter is equivalent to:

(a > b ? ' ' : b)=4;

Which is obviously illegal.

Upvotes: 2

Related Questions