D Logic bug
Steven Schveighoffer
schveiguy at gmail.com
Fri Oct 12 01:38:52 UTC 2018
On 10/11/18 9:16 PM, Jonathan Marler wrote:
> On Thursday, 11 October 2018 at 23:29:05 UTC, Steven Schveighoffer wrote:
>> On 10/11/18 7:17 PM, Jonathan Marler wrote:
>>
>>> I had a look at the table again, looks like the ternary operator is
>>> on there, just called the "conditional operator". And to clarify, D's
>>> operator precedence is close to C/C++ but doesn't match exactly.
>>> This is likely a result of the grammar differences rather than an
>>> intention one. For example, the "Conditional operator" in D actually
>>> has a higher priority than an assignment, but in C++ it's the same
>>> and is evaluated right-to-left. So this expression would be
>>> different in C++ and D:
>>>
>>
>> Not in my C/D code. It would have copious parentheses everywhere :)
>>
>
> Good :)
Yep. General rule of thumb for me after having been burned many many
times -- Always use parentheses to define order of operations when
dealing with bitwise operations (and, or, xor) and for the ternary operator.
I think I do make an exception when it's a simple assignment. i.e.:
a = cond ? 1 : 2;
>
>> That case is actually very strange, I don't know if it's something
>> that's really common.
>>
>
> Yes, that explains why myself, Jonathan Davis and certainly others
> didn't know there were actually differences between C++ and D Operator
> precedence :) I wasn't sure myself but having a quick look at each's
> operator precedence table made it easy to find an expression that
> behaves differently in both.
>
I actually was curious whether DMC followed the rules (hey, maybe Walter
just copied his existing code!), but it does follow C's rules.
-Steve
More information about the Digitalmars-d
mailing list