Enum type check in function call

P

Pritam

Facing a little issue with gcc ( maybe all c compiler have behaves
this way for the problem)

Got a function that takes in a enumerated type. Even if another
enumerated type is passed 'gcc' does not raise any warnings or error.
'g++' generates a error msg for the same. Is there a way to turn on
enum type checks while passing to functions in 'gcc'.


code snippet:


typedef enum {
gcc_enum_type1_val1,
gcc_enum_type1_val2,
gcc_enum_type1_val3,
} gcc_enum_type1_t;

typedef enum {
gcc_enum_type2_val1,
gcc_enum_type2_val2,
gcc_enum_type2_val3,
} gcc_enum_type2_t;


unsigned int foo(gcc_enum_type2_t val)
{
return (unsigned int) val;
}


int main()
{
unsigned int i;
i = foo(gcc_enum_type1_val1);
return 0;
}

gcc compiles the program without any issues.
'g++' complains:
xx.cpp: In function `int main()':
xx.cpp:nn: error: cannot convert `gcc_enum_type1_t' to
`gcc_enum_type2_t' for
argument `1' to `unsigned int foo(gcc_enum_type2_t)'

I want a similar type check in 'gcc' or it is not possible ic 'C'.

Thanks
Pritam
 
I

Ian Collins

Pritam said:
Facing a little issue with gcc ( maybe all c compiler have behaves
this way for the problem)

Got a function that takes in a enumerated type. Even if another
enumerated type is passed 'gcc' does not raise any warnings or error.
'g++' generates a error msg for the same. Is there a way to turn on
enum type checks while passing to functions in 'gcc'.
That's a "feature" of C, corrected in C++!

Enums aren't really types in C.
 
C

Chris Dollin

Richard said:
Are they not just ints?

The /constants/ are just ints, but the `enum wossname`s aren't; they're
(impoverished) types. I took Ian to mean the sort-of-type Enum, not the
named-elements-of.
 
I

Ian Collins

Chris said:
The /constants/ are just ints, but the `enum wossname`s aren't; they're
(impoverished) types. I took Ian to mean the sort-of-type Enum, not the
named-elements-of.
I did. I'll stick with "impoverished types", from now on :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top