Custom enumeration type

Can I determine the type that will be used as the base enumeration type? Something like that:

struct S { S(int i) : value(i) {} operator int() { return value; } int value; }; enum E : S { A, B, C }; 

The error message reports that S must be an integral type. I tried to specialize std::is_integral as follows, but it seems that in this context the "integral type" really means one of the main types.

 namespace std { template<> struct is_integral<S> : public true_type {}; } 

So, using any version of C ++, is there a way to make a custom type disconnected as an integral type?

+5
source share
2 answers

Is it possible to determine the type that will be used as the base enumeration type?

You can only use integral types to define enum s, not the old type.

For example, you can use

 enum E : char { A, B, C }; 

to indicate that the value of E will be of type char . But you cannot use

 enum E : S { A, B, C }; 

From C ++ 11 Standard, 3.9.1 / 7 :

The types are bool , char , char16_t , char32_t , wchar_t , and signed and unsigned integer types are collectively called integral types. A synonym for the integral type is the integer type.

+2
source

stand :: is_integral checks if T is an integral type. Contains a constant member value that is true if T is of type bool , char , char16_t , char32_t , wchar_t , short , int , long , long long or any extended integer types defined by the implementation, including any signed, unsigned, and cv-qualification options . Otherwise, the value is false.

This is an excerpt from here . This means that enum must use one of the following types:

bool , char , char16_t , char32_t , wchar_t , short , int , long , long long .

+2
source

Source: https://habr.com/ru/post/1268958/


All Articles