Enum type check in function call

Discussion in 'C Programming' started by Pritam, Mar 28, 2008.

  1. Pritam

    Pritam Guest

    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
     
    Pritam, Mar 28, 2008
    #1
    1. Advertising

  2. Pritam

    Ian Collins Guest

    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.
     
    Ian Collins, Mar 28, 2008
    #2
    1. Advertising

  3. Pritam

    Richard Guest

    Ian Collins <> 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?
     
    Richard, Mar 28, 2008
    #3
  4. Pritam

    Chris Dollin Guest

    Richard wrote:

    > Ian Collins <> 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
     
    Chris Dollin, Mar 28, 2008
    #4
  5. Pritam

    Ian Collins Guest

    Chris Dollin wrote:
    > Richard wrote:
    >
    >> Ian Collins <> 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.
     
    Ian Collins, Mar 28, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    568
  2. Eric Sosman
    Replies:
    3
    Views:
    795
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    512
  4. Lew
    Replies:
    0
    Views:
    545
  5. Roedy Green
    Replies:
    0
    Views:
    743
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page