warning message with GNU compiler
llewelly@xmission.com
llewelly@xmission.com
Wed May 5 19:20:00 GMT 2004
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++' .
More information about the Gcc-bugs
mailing list