Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is this valid C? Date: Sat, 25 Feb 2017 13:13:26 -0800 Organization: None to speak of Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="26f635ca6c8fd6198c9c9c2b8f050714"; logging-data="22293"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aJkA3Xay1WM7/EFp4Hd+M" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:H3RNuaQ5NsZSz7q6pUs0zblMYwc= sha1:xwl/RenVP+DETaqmtxSBPz/zCLQ= Xref: csiph.com comp.lang.c:104718 BartC writes: > Take this set of enum declarations: > > enum R {red}; > enum G {green}; > > enum {blue} *pblue; > enum {yellow} *pyellow; > > enum R *pred; > enum G *pgreen; > > pred=pgreen; > pblue=pyellow; > > > How seriously should those type mismatches in the assignments be taken? How seriously you should take them is entirely up to you. Both assignments are constraint violations. > Some compilers warn, others don't care. (And that should be no 'hard' > error because the underlying target type should be the same.) Type compatibility (see N1570 6.2.7) is not transitive. You can have three types A, B, and C, where A and B are compatible, B and C are compatible, but A and C are not. (The standard could stand to be more explicit on this point.) Each enum type is compatible with char, a signed integer type, or an unsigned integer type; the choice is implementation-defined (6.7.2.2p4). With the compiler I use, all your enum types happen to be compatible with unsigned int. (It would be legal, but silly, for enum R and enum G to be compatible with *different* integer types, and even for them to have different representations and sizes.) But even though enum R and enum G are very probably compatible with the same integer type, they are not compatible with each other. Their incompatibility doesn't matter if you're assigning a value of type enum R to an object of type enum G, since there's an implicit conversion between any two arithmetic types. But pointers to incompatible types are incompatible, and there is no implicit conversion between incompatible pointer types (other than void*). Any conforming compiler must issue at least one diagnostic for the assignments in your example. As always, that diagnostic can be a non-fatal warning. (My personal preference is for the diagnostic to be a fatal error; I can get that by using "gcc -pedantic-errors".) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"