I was under the impression that it was an error or at least a warning for an integer type to be passed into a function that requires an enum argument. But I tried it in Compiler Explorer with -Wall -Wextra -Werror
and there are no warnings. What am I missing? Does the C standard allow this?
Perhaps more importantly to my immediate use, is there a compiler option for gcc or clang to warn for this? The intent of having an enum
type as a formal argument is as a contract to restrict to valid enum
values.
#include <stdint.h>
typedef enum {
FLAG_A = 1,
FLAG_B = 2,
FLAG_C = 4
} FLAG;
uint16_t flags = 0;
void clearFlags(FLAG flag)
{
flags &= ~flag;
}
void doit()
{
clearFlags(flags); // this should not be ok; flags is type uint16_t
}