This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: warning message with GNU compiler


Jim Wilson <wilson@specifixinc.com> writes:

> Keren Zik-Meirom wrote:
> > I am trying to generate a warning message in case where an object
> > of particular enumeration type is assigned something other that
> > one of its constant, from the tests I've made I could not
> > reproduce it. 
> 
> We don't have a warning option for that.  The ISO C language does not
> place restrictions on what values can be assigned to a variable of
> type enum.
> 
> You didn't specify the language you are using.  C++ rules are
> different from C rules, but I am not very familiar with C++ rules.  I
> don't see a C++ option for this either.

C++ requires an explicit cast to assign an integer to an enum. That
    is:

    enum color{red, green, blue};

    int main()
      {
        enum color c; 
        c= 1; //error.
      }

    results in:
    g++ -O0 -g enum2.c
    enum2.c: In function 'int main()':
    enum2.c:6: error: invalid conversion from 'int' to 'color'

whereas the same code is well-formed C.

One answer to the OP's question is 'try using C++' .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]