Motivation for scoped enum comparison behavior?

Hi all,

I hope this is the correct mailing list.

I wanted to ask why clang chooses to compare scoped enumerators as if they were unsigned numbers, even when the underlying type is signed. For example

enum class E : int
{
neg_one = -1,
one = 1;
};

neg_one > one; // true

What is the motivation for this behavior?

Best,

Eric

Eric Fiselier <eric@efcs.ca> writes:

I wanted to ask why clang chooses to compare scoped enumerators as if they
were unsigned numbers, even when the underlying type is signed. For example

enum class E : int
{
   neg_one = -1,
   one = 1;
};

neg_one > one; // true

What version of clang are you using? I don't see the behaviour you're
seeing.

This was a bug, and is already fixed. (I can look up the bug number if
you're interested, but in essence the problem was that we were failing to
determine that 'E' is signed.)

Thanks Richard, I was unsure if it was a bug since the standard seems to be
ambiguous about how to compare scoped enumerations (making no mention about
the underlying type).

Hi Eric,
In the Standard at [expr]

<5 note 9>
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

— If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.

enjoy,
Karen