Lawrence D'Oliveiro <l...@nz.invalid> writes:
> Operator precedence in C, particularly in regard to the bitwise operators,
> can be a bit awkward. Consider an expression like
>
> (val & check_bits) == set_bits
>
> which uses ?check_bits? to mask out the bits to check from ?val?, and then
> compares the result to ?set_bits?. A more natural precedence order would
> allow this to be written without the parentheses:
>
> val & check_bits == set_bits
>
> I discovered a few years ago (quite by accident) that Python does make a
> small change to the operator precedences to allow exactly this sort of
> thing.
>
> My question is, if this corresponding operator precedence change were to
> be made in C, is there any actual real-world code that would break?
Surely the answer to that question is an emphatic yes, but in any
case it's the wrong question.
An obvious first question is, if such a change were made to C
(via changes to the ISO C standard), would it result an any
significant gain? The answer to that question is No. Some
things would be easier, some things would be harder, the overall
effect on programming effort might be a net positive, but it is
as best a second-order effect: possibly a gain, but not a
significant gain.
An obvious next question is, if such a change were made to C, how
much collateral damage would result? The answer to that question
is hard to know exactly, but without doubt it is at least huge.
Consider what happened with C99. For the most part C99 was upward
compatible with C90, with two exceptions: removal of implicit
function declarations, and removal of implicit int. Furthermore
those changes would cause diagnostics if run on C90 code that
didn't fit the C99 rules. Despite that it took a very long time,
probably at least a decade, before C99 was fully embraced by the
C community. The effect of a change in precedence relationships
surely would be much, much longer. An important principle in the
evolution of C is that working code keeps working. There are some
exceptions to that principle, but they are rare and modest in
scale. The proposed precedence change would not be an evolution
of the language but a bifurcation of the language. In this case
the "cure" is worse than the disease.