473,325 Members | 2,816 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,325 software developers and data experts.

enum bound check in compiler time in C

Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.I am keeping some space between two types
of events. now, the problem is How to check compile time that first
type of events are now overlapped with second type?

Is there anyway to check for enums in compile time?

any help would be appriciated.

Thanks in advance,
Parag

Apr 23 '07 #1
5 4071
pa*********@gmail.com wrote On 04/23/07 10:55,:
Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.I am keeping some space between two types
of events. now, the problem is How to check compile time that first
type of events are now overlapped with second type?

Is there anyway to check for enums in compile time?
The preprocessor cannot do it because the enum constants
don't even have any values when the preprocessor is operating.
However, there is an ugly hack:

enum { EVENT1 = 0, EVENT1a, EVENT1b, ..., EVENT1z,
EVENT2 = 20, EVENT2a, EVENT2b, ... };
/* NOTE to code maintainers: If the following line
* produces a compile error, the problem is that
* the EVENTxxx values need to be rearranged.
*/
static char unused[EVENT2 EVENT1z ? 1 : -1];

My suspicion is that you might be better off using an
entirely different approach. What is it about your current
scheme that requires EVENT2's numeric value to remain fixed
even as the program changes around it?

--
Er*********@sun.com
Apr 23 '07 #2
On 23 Apr, 15:55, paragbor...@gmail.com wrote:
Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.
Sounds like your design is wrong, from where I'm sitting. Either
they're one category of thing, in which case you have one variable
and one set of enums, or they're two categories of thing, in which
case you have two variables and two sets of enums.

If you need to track two different categories of thing with one
variable, you should probably use a structure containing category
and the enum value.
I am keeping some space between two types
of events. now, the problem is How to check compile time that first
type of events are now overlapped with second type?
I don't think you can.
Is there anyway to check for enums in compile time?
I don't believe so.

Apr 23 '07 #3
pa*********@gmail.com wrote:
Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.I am keeping some space between two types
of events. now, the problem is How to check compile time that first
type of events are now overlapped with second type?

Is there anyway to check for enums in compile time?
since you have only two types of enums, why don't you use positive and
negative values to make the difference?

For compile-time test, google for "static assert in C" but as a general,
portable and simple way to do, you could start with:

#define STATIC_ASSERT(expr) \
enum { Failed_STATIC_ASSERT_line_ ## __LINE__ = 1/(expr) }

STATIC_ASSERT( last_enum1 < first_enum2 );

This is what I use in COS.
any help would be appriciated.
hope this help.

a+, ld.
Apr 23 '07 #4
Le 23-04-2007, pa*********@gmail.com <pa*********@gmail.coma écrit*:
Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.I am keeping some space between two types
of events.
Why did you 'keep space between' ?

enum parag_event { ev_type1_1, ev_type1_2, ev_type1_3,
ev_type2_1, ev_type2_2 };

If one day you want to add one new ev_type1_4, just add it.

Marc Boyer
Apr 23 '07 #5
On Mon, 23 Apr 2007 07:55:05 -0700, paragborkar wrote:
Hello friends,

I am newbie in C. I have one enum variable for events. There are 2
types of events and I want to split them, but one can add events enums
later for the both the types.I am keeping some space between two types
of events. now, the problem is How to check compile time that first
type of events are now overlapped with second type?
<snip>
You could avoid overlap like this
enum
{ E1_1, E1_2
/* add type 1 events before this */
, NUM_E1 /* not an event */
, E2_1=NUM_E1 /* first type 2 events, add type2's after this */
, E2_2, E2_3
};

Apr 23 '07 #6

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

Similar topics

12
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
5
by: DJTB | last post by:
Dear Group, I'd like to check if a value is defined in an enum. Example: ------------------------------------------------------ typedef enum { A_VALUE = 1,
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
8
by: Mark P | last post by:
I'm working on a project where I have something like enum Modes {mode_a, mode_b, ...}; Due to project spec changes, the number of Modes has increased several times. There are a few places...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.