Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the scoped enum support operator '<' by default?

Tags:

c++

enums

Consider:

enum class Number {one, two};

if (Number::one < Number::two)
{}

My understanding is that the scoped enum needs to be cased into the underlying type or integer, and then it can be applied to operator < > ==. But it looks like the code snippet above can work without any explicit overloading operator <.

I don't see any descriptions in Enumeration declaration.

What does the C++ standard say about which operators are supported for a scoped enum by default?

like image 217
SSY Avatar asked Jan 28 '19 14:01

SSY


People also ask

What is a scoped enum?

A scoped enum looks exactly as a traditional enum except that the keyword class (or struct – the two keywords are interchangeable in this context) appears between the keyword enum and the enum name, as shown in the following example: enum class Color //C++11 scoped enum.

How do you declare an enum in C++?

An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used. enum season { spring, summer, autumn, winter }; Here, the name of the enumeration is season .

What is an enum class in C++?

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. This article covers the ISO Standard C++ Language enum type and the scoped (or strongly-typed) enum class type which is introduced in C++11.


2 Answers

If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:

[expr]

11 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, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.

So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.

like image 95
StoryTeller - Unslander Monica Avatar answered Sep 29 '22 13:09

StoryTeller - Unslander Monica


My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.

Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.

[expr.rel]p6:

If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.

like image 25
Rakete1111 Avatar answered Sep 29 '22 12:09

Rakete1111