473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

enums & ints

Intel icc seems to think that enums are not ints:

#include <stdlib.h>

typedef enum { TRUE=1, FALSE=0 } Truth;

#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
Truth flg=FALSE;
*screen = !flg;
return 0;
}

flg.c(10): warning #188: enumerated type mixed with another type
*screen = !flg;
^
But as far as I can read the standard, this should be perfectly
legitimate. Can anyone shed light on this?

Victor.

--
Victor Eijkhout -- eijkhout at tacc utexas edu
Mar 16 '07 #1
7 8915
In article <1h*************************@sig.for.address>,
Victor Eijkhout <se*@sig.for.addresswrote:
>Intel icc seems to think that enums are not ints:
>#include <stdlib.h>
typedef enum { TRUE=1, FALSE=0 } Truth;
#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
Truth flg=FALSE;
*screen = !flg;
return 0;
}
>flg.c(10): warning #188: enumerated type mixed with another type
*screen = !flg;
^
>But as far as I can read the standard, this should be perfectly
legitimate. Can anyone shed light on this?
It's a warning message, not an error message. It's pointing
out to you that you are doing something that is suspect.
Compilers are allowed to emit any number of extra warnings
(as long as they compile programs that do not have constraint
violations).
--
Programming is what happens while you're busy making other plans.
Mar 16 '07 #2
(Walter Roberson) wrote:
>Victor Eijkhout wrote:
>>Intel icc seems to think that enums are not ints:
>>#include <stdlib.h>
typedef enum { TRUE=1, FALSE=0 } Truth;
#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
Truth flg=FALSE;
*screen = !flg;
return 0;
}
>>flg.c(10): warning #188: enumerated type mixed with another type
*screen = !flg;
^
>>But as far as I can read the standard, this should be perfectly
legitimate. Can anyone shed light on this?

It's a warning message, not an error message. It's pointing
out to you that you are doing something that is suspect.
Compilers are allowed to emit any number of extra warnings
(as long as they compile programs that do not have constraint
violations).
In this case it is a useful warning. In the general case, enum types
are not semantically compatible, even if they are "all integers".
For example:

typedef enum { OPEN, CLOSED } valve_state;
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}
...
}


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Mar 16 '07 #3
Roberto Waltman <us****@rwaltman.netwrote:
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}
Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.

Victor.

--
Victor Eijkhout -- eijkhout at tacc utexas edu
Mar 16 '07 #4
Victor Eijkhout wrote:
Roberto Waltman <us****@rwaltman.netwrote:
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}

Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.
Your original example implicitly converted an int to an enum.

Mar 16 '07 #5
Victor Eijkhout wrote:
Roberto Waltman <us****@rwaltman.netwrote:
>>void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}


Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.
I often disagree with my compiler about what should be warned. Many
compilers let you turn off specific warnings. Use that feature to
maximize you compiling experience! I make minor code changes "to make
the compiler happy" so I don't need to turn off lots of warnings.

--
Thad
Mar 17 '07 #6
On Fri, 16 Mar 2007 15:47:54 -0500, se*@sig.for.address (Victor
Eijkhout) wrote in comp.lang.c:
Roberto Waltman <us****@rwaltman.netwrote:
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}

Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
There is no such thing as an "automatic cast" or an "implicit cast" in
C. The only thing that ever performs a cast is a cast operator.

C has conversions. Some of them will occur automatically in
expression, on assignment, and on passing arguments to functions.
Others will only occur when a cast operator is used.

A cast is defined as an "explicit conversion". Any conversion that
occurs without the use of a cast operator is not any type of cast.
indeed by objectionable.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 17 '07 #7
Thad Smith <Th*******@acm.orgwrote:
Many
compilers let you turn off specific warnings. Use that feature to
maximize you compiling experience!
That's what I've settled on. "icc -wr188"
I make minor code changes "to make
the compiler happy" so I don't need to turn off lots of warnings.
As do I. Unfortunately here I'm having to install a 1000-or-so-file
library, not written by me.

Victor.

--
Victor Eijkhout -- eijkhout at tacc utexas edu
Mar 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
4
by: David Rasmussen | last post by:
The problem comes up in all sorts of contexts. But here is an example: We want to model the basics of chess, maybe for making a chess program, or maybe just to make an interactive board or ... ...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
6
by: Noah Roberts | last post by:
I am having an interesting compilation error that makes me wonder if enums can conflict with each other. For example: class A { enum X { TEST, TEST2 }; public: ....
1
by: Rajesh Abraham | last post by:
I am running loop with calls different functions such as SetHomeDrive, AddUserToGroup etc. Now if any of the functions fails, I am just catching and throwing a new error like throw new...
4
by: Craig | last post by:
Newbie... I'm casting enums back and forth to ints so to use them as an index into a complex two dimensional array. I thought it would help readability. Instead, my code has casts all over the...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Cartoper | last post by:
I am working in VS2005 (.Net 2.0). I have a unmanaged C++ class that will be used by C#, once I get to that point. I would like to throw a System::ApplicationException from the unmanaged code,...
2
by: Spam Catcher | last post by:
Hi all, I have a usercontrol with a settable enum. How do I allow the user to edit the enum through VS.NET's property panel? The enum shows up, but it's not currently editable. Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.