dmd-2.067.0-b1
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 13 05:25:54 PST 2015
On 2/13/15 7:38 AM, tcak wrote:
> On Friday, 13 February 2015 at 09:38:04 UTC, Dennis Ritchie wrote:
>> This is a bug?
>>
>> import std.stdio;
>>
>> void main() {
>> int a = 0;
>>
>> writeln( (a < 10) ? a = 1 : a = 2 ); // prints 2
>>
>> writeln( (a < 10) ? a = 1 : (a = 2) ); // prints 1
>> }
>>
>> Even C++ output:
>> 1
>> 1
>
> About 2 years ago, I had a problem with similar structure.
>
> My guess is that the first one is accepted as this.
>
> ((a < 10) ? a = 1 : a) = ( 2 )
>
> Thereby it gives this result. Vague definitions are always error prone.
Yes, the operator precedence (curiously not defined in the spec) is here:
http://wiki.dlang.org/Operator_precedence
Conditional operator is above assignment operators.
C++ operator precedence is here:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
Note they are the same, but I think C++ ?: = operator in this case does
not result in lvalue. So it must be that the = cannot bind to the result
of the operator, therefore it binds to the a. Interesting...
-Steve
More information about the Digitalmars-d-learn
mailing list