that is bug?

kdevel kdevel at vogtner.de
Sun Apr 8 10:03:33 UTC 2018


On Sunday, 8 April 2018 at 07:22:19 UTC, Patrick Schluter wrote:
>> You may find an in-depth discussion of the C++ case in
>>
>> https://stackoverflow.com/questions/7499400/ternary-conditional-and-assignment-operator-precedence
>
> My formulation was ambiguous, it is the same precedence as the 
> link says. The link also says that's it's right to left 
> evaluation. This means that for expression:
>
>     a ? b = c : d = e;
>
>
> right to left evaluation will make the = e assignment higher 
> priority than the b = c assignment or the ternary even if they 
> have the same priority level.

To summarize: C++ works as expected and C prevents the assigment 
because the conditional operator does not yield an l-value:

ccondo1.c
---
int main ()
{
    int a, b;
    1 ? a = 1 : b = 2;
    return 0;
}
---

$ cc ccondo1.c
ccondo1.c: In function 'main':
ccondo1.c:4: error: lvalue required as left operand of assignment

Other languages:
----------------

- go: has no ternary conditional

- Java: Same as in C, example does not compile due to missing 
l-value.

- JS: Like C++ (!).

   
https://www.ecma-international.org/ecma-262/6.0/#sec-conditional-operator

"The grammar for a ConditionalExpression in ECMAScript is 
slightly different from that in C and Java, which each allow the 
second subexpression to be an Expression but restrict the third 
expression to be a ConditionalExpression. The motivation for this 
difference in ECMAScript is to allow an assignment expression to 
be governed by either arm of a conditional and to eliminate the 
confusing and fairly useless case of a comma expression as the 
centre expression."



More information about the Digitalmars-d mailing list