gcc doesn't seem to produce a warning with the following code. How can I get it to produce a warning?
typedef enum
{
REG8_A,
REG8_B,
REG8_C
}REG8;
typedef enum
{
REG16_A,
REG16_B,
REG16_C
}REG16;
void function(REG8 reg8)
{
}
int main(void)
{
function(REG16_A); // Should warn about wrong enum
}
The reason of such behaviour is that you are using C compiler rather than C++. And in C enum types are not really types, enums in C just hold int constants and they can be freely mixed with whatever integers and whatever arithmetic.
In C++ instead you have real enums as you would like to think of them, and the needed typechecking occurs as conveyed by the language standard.
Your problem can be resolved in two ways:
Use C++ compiler.
This way you will have real enums, as you want them.
Change your code to be in pure-C style, i.e. not using enums, as in C they are merely constant sets where the compiler only helps you to order the constant values. And in C you will be the one responsible to keep the "types" of passed constants consistent. Once again: for C, enum members are just int constants, you can't make them typed.
#define REG8_A 0
#define REG8_B 1
#define REG8_C 2
#define REG16_A 0
#define REG16_B 1
#define REG16_C 2