Enum class' underlying type aliased to integral type (compile error)

797 Views Asked by At

I compile this with GCC 7.2.0:

typedef float Signal_t;

enum class Signal_level : Signal_t {
  low = -1.0, neutral = 0.0, high = 1.0
};

the compiler response is:

error: underlying type ‘Signal_t {aka float}’ of ‘Signal_level’ must be an integral type                                                                                             

Is this behavior dictated by the standard (-std=c++17) or is specific to GCC? I'd expect GCC to recognize Signal_t as an integral type.

1

There are 1 best solutions below

0
Rakete1111 On

According to [dcl.enum]p2:

The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored.

Here the type-specifier-seq refers to the part after :.

And what are integral types ([basic.types]p7):

Types bool, char, char16_­t, char32_­t, wchar_­t, and the signed and unsigned integer types are collectively called integral types.47 A synonym for integral type is integer type.

So a float is not part of the integral types, and so is not valid to use in an enum as base.