Hello,

i don't know whether this was already requested way back in 1985, and maybe
there is an evangelical answer to this. 

This is a request to add a compiler option for warnings if the evaluation of
operator& and operator== (and similar) may be not 'as expected'. I personally
feel it in most cases utter nonsens to apply operator& on a boolean result, as
it is defined in the C/C++ operator precedence hierarchy; and every now and
then i forget to add brackets around each and every operation in doubt in an
expression and then i'm puzzled for hours what goes wrong.

Example:

  if( value&mask == $7F00 ) { … }

does not evaluate as expected and, in my opionion, the only reasonable way, but
evaluates as:

  if( value & (mask==$7F00) ) { … }

The reasoning for adding this warning is comparable to the reasoning which lead
to add a warning if the result of operator= is used as part of the boolean
expression in "if(…){…}": It is _probably_ not what the programmer intended.
The way to circumvent the warning (if enabled) would be the same as for
operator= too: add brackets around the expression. Eventually these warnings
could even be combined into a single compiler option, but they probably should
go into the same compiler option sets.

Exact scope for this kind of warning should be:

  operator &, ^ and |   versus   operator == and !=

because it makes no sense to apply a bit masking operator on a boolean result,
as it is done if no brackets are used to reorder the sequence of evaluation.

And the same applies in my eyes to:

  operator << and >>   versus   operator +, -, *, / and %

because operator<< and >> do an exponentation 2**n and the priority of
exponentation is (should be) higher than that of multiplication.

The C/C++ standard cannot be changed, though it handles it wrong in my opinion,
but adding a warning if the default evaluation order is applied to 2 operations
from the above sets would be very appreciated in my eyes.

Thanks for an answer,

    ... kio !


-- 
           Summary: warning for potential undesired operator& and operator==
                    evaluation order
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bugzilla at little-bat dot de
 GCC build triplet: (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
  GCC host triplet: powerpc-apple-darwin8-g++-4.0.1


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32270

Reply via email to