[cfe-dev] Possibly invalid enum tautology warning

Douglas Gregor dgregor at apple.com
Mon Aug 8 10:16:38 PDT 2011


On Aug 8, 2011, at 9:31 AM, David Blaikie wrote:

>>> enum tester { A = 0, B };
> 
> Ah, I missed this bit. But I suspect that even without this ("= 0")
> you'd get the same behavior - at that point I'd be more inclined to
> consider Peter's position, assuming it's possible that a compliant
> implementation could use A = -1, B = 0, for example.
> 
> If it's guaranteed that unspecified enum values start at zero,

C99 6.7.2.2p3 specifies "If the first enumerator has no =, the value of its enumeration constant is 0." C++ does the same.

> and as
> has been implied/mentioned, assigning an enum does have UB when
> assigned outside the range of enumerated values (much like wedging 2
> into a bool produces crazy) then, yes, this warning seems totally fine
> to me. It's not complete, of course, but it's not wrong either.

C99 and C++98/03 say nothing about this. C++0x tried to clean it up a little based on what the committee felt was the intent in C and C++. See C++0x N3290 7.2 [dcl.enum]p7 for the full details, but the relevant conclusion is that, if the smallest enumerator is non-negative, then the values allowed in the enumeration are all non-negative.

	- Doug

[*] whose underlying type is not fixed, as is the case here.



More information about the cfe-dev mailing list