Is the counter used in the expression of the same type as the base type of its enumeration?

What is the type of enum constant when used outside the definition of unumed enum?

Consider the following code:

#include <iostream>

enum modes
{
    begin = 0,
    end = 1
};

int main()
{
    std::cout << std::boolalpha
        << std::is_same<unsigned int, typename std::underlying_type<modes>::type>::value
        << std::endl;
    std::cout << sizeof(modes) << std::endl;
    std::cout << (-100 + end) << std::endl;
}

This gives on my machine:

true
4
-99

Now, if I only changed the value of any other enumerator, beginto 2147483648, then my output would be:

true
4
4294967197

Apparently, this means that the type endhas changed from intto unsigned int, even the base type is modesstill the same (i.e. unsigned int).

Are there special rules for integral promotions regarding transfers?

+4
source share
2

++ ( 7.2 )

  1. ... ....

(4.5 )

3 , (7.2) prvalue , ( bmin bmax, 7.2): int, unsigned int, long int, unsigned long int, long long int unsigned long long int. , prvalue (4.13), . , .

+7

, unumed enum?

, . begin - modes. , .

?

. enum, . enum, enum, . int ? , int.

+2

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


All Articles