0

I always thought that in C++, as well as in C, enum are just integers. But today I got this "invalid conversion from int to E":

enum E {
    FIRST = 0b01,
    SECOND = 0b10,
    THIRD = FIRST | SECOND  // this is fine
};

int main()
{
    E first = FIRST;
    E third = FIRST | SECOND; // this is NOT fine

    return 0;
}
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64

1 Answers1

0

You should cast it cause it has type int so This should achieve your goal

E first = FIRST;
E third = static_cast<E>(FIRST | SECOND);
Spinkoo
  • 2,080
  • 1
  • 7
  • 23