Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Enum type check in function call

Reply
Thread Tools

Enum type check in function call

 
 
Pritam
Guest
Posts: n/a
 
      03-28-2008
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-28-2008
Pritam wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      03-28-2008
Ian Collins <ian-> writes:

> Pritam wrote:
>> 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.


Are they not just ints?
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      03-28-2008
Richard wrote:

> Ian Collins <ian-> writes:
>
>> Pritam wrote:
>>> 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.

>
> 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.

--
"What would that matter, if it made a good book?" /Gaudy Night/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-28-2008
Chris Dollin wrote:
> Richard wrote:
>
>> Ian Collins <ian-> writes:
>>
>>> Pritam wrote:
>>>> 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.

>> 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 did. I'll stick with "impoverished types", from now on

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: an enum question - how to convert enum type to a string? Roedy Green Java 0 02-27-2010 04:52 AM
Re: an enum question - how to convert enum type to a string? Wojtek Java 1 02-27-2010 03:44 AM
Re: an enum question - how to convert enum type to a string? Lew Java 0 02-27-2010 03:40 AM
Re: an enum question - how to convert enum type to a string? Eric Sosman Java 3 02-26-2010 03:36 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57