f



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

0
4/23/2007 2:55:05 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

5 Replies
340 Views

Similar Articles

[PageSpeed] 50

paragborkar@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?

-- 
Eric.Sosman@sun.com
0
Eric.Sosman (4552)
4/23/2007 3:21:08 PM
Le 23-04-2007, paragborkar@gmail.com <paragborkar@gmail.com> a �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
0
Marc.Boyer1 (129)
4/23/2007 3:24:25 PM
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.

0
mark_bluemel (848)
4/23/2007 3:27:00 PM
paragborkar@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.
0
4/23/2007 3:29:17 PM
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
};

0
dmuir (45)
4/23/2007 4:53:07 PM
Reply: