On Thursday, 11 October 2018 at 14:35:34 UTC, James Japherson wrote:
Took me about an hour to track this one down!

A + (B == 0) ? 0 : C;

D is evaluating it as

(A + (B == 0)) ? 0 : C;


The whole point of the parenthesis was to associate.

I usually explicitly associate precisely because of this!

A + ((B == 0) ? 0 : C);

In the ternary operator it should treat parenthesis directly to the left as the argument.

Of course, I doubt this will get fixed but it should be noted so other don't step in the same poo.

In c++ the ternary operator is the second most lowest precedence operator, just above the comma. You can see a table of each operator and their precendence here, I refer to it every so often: https://en.cppreference.com/w/cpp/language/operator_precedence

Learning that the ternary operator has such a low precedence is one of those things that all programmers eventually run into...welcome to the club :)

It looks like D has a similar table here (https://wiki.dlang.org/Operator_precedence). However, it doesn't appear to have the ternary operator in there. On that note, D would take it's precedence order from C/C++ unless there's a VERY good reason to change it.

Reply via email to