Why does D change operator precedence according to C/C++ ?

Stewart Gordon smjg_1998 at yahoo.com
Mon Apr 2 04:10:05 PDT 2012


On 02/04/2012 11:32, Simen Kjaeraas wrote:
> On Mon, 02 Apr 2012 12:03:14 +0200, deadalnix <deadalnix at gmail.com> wrote:
>> I have a design question, or maybe it is a bug ? In D, == and != have the same
>> precedence than comparisons operators. This isn't the case in C/C++ . Is it a design
>> decision, made on purpose ? Is it a bug ?
<snip>
> It's there to force proper parenthezation:
<snip>

That combined with associativity rules.

In C(++),
     a < b == c <= d
means
     (a < b) == (c <= d)

However, if one simply made all the comparison operators equal precedence, then the 
meaning of this would change to
     ((a < b) == c) <= d

However, what has actually been done is to make these operators non-associative as well, 
in order to render it an illegal expression.  This was done to prevent subtle typos or 
confusion with other languages/notations where chaining of comparison operators denotes a 
conjunction of the comparisons.

Another way to look at it is that the different comparison operators have no precedence 
relative to each other (though each still needs to be non-associative).  Indeed, another 
change that has taken place is to make the comparison operators have no precedence 
relative to the bitwise boolean operators, simply because the C precedence rules here were 
confusing.
http://d.puremagic.com/issues/show_bug.cgi?id=4077

Stewart.


More information about the Digitalmars-d mailing list