Why it is legal to compare scoped enumerations

Viewed 86

Although scoped enumerations (enum class) cannot be implicitly converted to integral types, I still can compare them by < (on GCC 10.3).

#include <algorithm>
#include <iostream>

enum class Colours {
    Red = 0,
    Green = 1,
    Blue = 2
};

int main() {
    std::cout << (std::min(Colours::Blue, Colours::Red) < Colours::Green) << std::endl;
    return 0;
}

Why is this standard behaviour (if it is)?

Could you give me a reference to cppreference.com or c++ standard?

1 Answers

This is described in comparison operators

Arithmetic comparison operators

If the operands have arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed on both operands following the rules for arithmetic operators. The values are compared after conversions:

So in addition to arithmetic types (including integral types), scoped and unscoped enum types are explicitly mentioned.

Related