3 ms·
C enum values are convertible from int; C++ enum values aren't. This is one of the biggest differences in fairly idiomatic C code and has been the case for a ve
by barrkel 8y ago
C enum values are convertible from int; C++ enum values aren't. This is one of the biggest differences in fairly idiomatic C code and has been the case for a very long time (i.e. not dependent on newer C features not being in C++).
#include <stdio.h>
typedef enum EFoo {
FOO_A = 1,
FOO_B = 2
} TFoo;
int main()
{
TFoo foo = FOO_A | FOO_B;
printf("foo = %d\n", foo);
}
This compiles in C but not C++.
- elteto 8y agoWhat? https://godbolt.org/z/vnwvch https://godbolt.org/z/vnwvch
- antaflos 8y agoThat's not the same code, is it? GP's code does not seem to compile in C++: https://godbolt.org/z/5oAIYE https://godbolt.org/z/5oAIYE But it does in C: https://godbolt.org/z/zdTKiE https://godbolt.org/z/zdTKiE
- adrianN 8y agoTry creating a foo https://godbolt.org/z/AlHDhg https://godbolt.org/z/AlHDhg