With this code:
enum A { _1 };
enum B { _2 };
void f(A) {}
int main()
{
f(_2);
}
A C++ compiler complains that couldn't convert B to A (try it on Wandbox), But with a C compiler we just get a warning (I know C++ is not C):
main.c: In function ‘f’:
main.c:11:6: warning: type of ‘A’ defaults to ‘int’ [-Wimplicit-int]
11 | void f(A)
| ^
main.c:11:6: warning: unused parameter ‘A’ [-Wunused-parameter]
Which it's make sense (for me) because as far as I know: An enum is just an int (whatever short int, int, ... depending on the value of the enumerations). So:
- Why the C++ compiler doesn't let me use
BasA? - And it's fine to use
f(static_cast<A>(_2));?
My environment:
- Compiler:
GCC 11.1.0 - Compiler Flags:
-Wall -Wextra -Wpedantic - OS:
ArchLinux 5.13.13-arch1-1