What is the default value of enum class variable without initialization? The following code prints weird result.
#include <iostream>
#include <cstdio>
using namespace std;
struct C {
C() {
m_a = 1;
}
enum class E:bool {
a = false,
b = true,
};
int m_a;
E m_e;
};
int main()
{
C c;
cin >> c.m_a;
printf("m_e: %d; compare to C::E::a: %d\n", static_cast<int>(c.m_e), c.m_e == C::E::a);
return 0;
}
Build with gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
g++ -g -o c c.cpp
# run, input 1 and get the result.
./c
1
m_e: 252; compare to C::E::a: 253
# run c, input 2 and get the result.
./c
2
m_e: 254; compare to C::E::a: 255
Q1: Why m_e is not initialized as C::E::a (value false == 0?) or C::E::b (value true == 1?)
Q2: Why dose the comparison expression c.m_e == C::E::a return 253/255, but not false (value 0) or true (value 1)?