g++ 2.96 -W issues bogus warning for conditional expression with enum and unsigned

Martin Sebor sebor@roguewave.com
Thu Jul 6 12:59:00 GMT 2000


Nathan Sidwell wrote:
> 
> Nathan Sidwell wrote:
> > However, looking at Martin Sebor's example, the enumeration should be
> > unsigned, so the warning should not be triggered -- I'll take a look.
> Ok, digging deeper I find code in type_promotes_to (cvt.c), which
> converts to int, if that can represent all the values of the enumeration.
> However, I believe that is incorrect.
> 
> [4.5]/2 says an enumeration can be implicitly converted to the first of
> `int', `unsigned int', `long' or `unsigned long' that can represent all
> the values of the underlying type. I.e. not simply all the values of the
> enumeration.
> 
> [7.2]/5 says the underlying type is an integral type that can represent
> all the enumerator values. It is implementation define as to which integral
> type is chosen.
> 
> We choose `unsigned int' for enumerations with non-negative values,
> so those should promote to `unsigned int', not `int'. [4.5]/2 would
> promote an `unsigned short' underlying type to `int' in preference to
> `unsigned int', if we had such things.

What difference does it make wrt to the warning?

Looking at the test case at
http://gcc.gnu.org/ml/gcc-patches/1999-08n/msg00843.html , I don't
believe that the warning should be issued for mixed enums at all, i.e.,
in particular:

  j = (i ? e1 : e2);    // WARNING - mismatch
  d = (i ? e1 : 1.0);   // WARNING - mismatch
  d = (i ? 1.0 : e2);   // WARNING - mismatch

are IMO all well-formed and the type of the conditional-expression is
some integral type (5.16, p6, bullet 2),

while

  (i ? (void)1 : i++);        // WARNING - not a throw
  (i ? i++ : (void)1);        // WARNING - not a throw

should produce errors (not just warnings), according to 5.16, p2, since
neither bullet holds.

Martin

> 
> Is there a language lawyer in the house?
> 
> nathan
> 
> --
> Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
>          'But that's a lie.' - 'Yes it is. What's your point?'
> nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


More information about the Gcc-bugs mailing list