- compiler : clang++
- c++ standard : c++20
I tried to run the code, and the results met my expectations very well.
#include <iostream>
using namespace std;
int main()
{
enum class Num : int
{
one = 1,
two = 2,
zero = 0
};
Num e = Num::one;
auto a = static_cast<std::underlying_type_t<Num>>(e);
cout << "-----------" << endl;
cout << a << endl;
cout << "-----------" << endl;
return 0;
}
result:
-----------
1
-----------
But when I made a small modification, changing the underlying type of enum class from int to int8_t,the result was unexpected.
#include <iostream>
using namespace std;
int main()
{
enum class Num : int8_t // from 'int' to 'int8_t'
{
one = 1,
two = 2,
zero = 0
};
Num e = Num::one;
auto a = static_cast<std::underlying_type_t<Num>>(e);
cout << "-----------" << endl;
cout << a << endl;
cout << "-----------" << endl;
return 0;
}
result:
-----------
-----------
What happened here?