Public Group

# int or enum to enum safety [C++]

This topic is 3416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there an easy way to check that a value from one enum type (hence effectively an int), is valid in another enum (without a massive switch statement)?

##### Share on other sites
If your enum values are sequential, then you just need to check that the value is >= min and < max. Although, why do you need to do this?

##### Share on other sites
It depends on how you've defined up your enumerations. If you define them so that they are numbered consecutively you can use one if statement to make sure a variable is in the proper range. For example,

enum SomeEnum{    VALUE_1 = 1,    VALUE_2 = 2,    VALUE_3 = 3,    VALUE_4 = 4};if ((someVariable >= VALUE_1) && (someVariable <= VALUE_4)){    // someVariable is in range}else{    // someVariable is out of range}

This doesn't work if your enum has gaps, which is sometimes unavoidable.

enum SomeEnum{    VALUE_1 = 1,    VALUE_2 = 2,    VALUE_3 = 4,    VALUE_4 = 8};if ((someVariable >= VALUE_1) && (someVariable <= VALUE_4)){    // someVariable is between the extreme values but doesn't necessarily     // correspond to an enum value}else{    // someVariable is definitely out of range}

To make it even easier I usually do something like the following:

enum SomeEnum{    VALUE_1,    VALUE_2,    VALUE_3,    VALUE_4,    NUMBER_OF_VALUES,    FIRST_VALUE = VALUE_1};if ((someVariable >= FIRST_VALUE) && (someVariable < NUMBER_OF_VALUE)){    // someVariable is in range}else{    // someVariable is out of range}

In my opinion the biggest advantage to doing it that way is that large enums, you only need one if check to see if a variable is in range, and by abstracting the starting value and the number of enum members if you ever have to change the enumeration you don't have to hunt through your code to change the range checks.

##### Share on other sites
Surely you can always cast the enum value to an integer given that the other enum has the same value declared at the same position as the first enum. If you can't guarantee this (and you can't because when extending one of the enums there will be times when you forget to adjust the other one) then you would have to do a big if else if ... cascade to find this out.

As you can already see from those explanations this is something you should not do. If you need to check the containment of one value in two different enums it sounds as if you have choosen a bad design.

##### Share on other sites
Quote:
 Original post by powell0It depends on how you've defined up your enumerations. If you define them so that they are numbered consecutively you can use one if statement to make sure a variable is in the proper range.

Well, correct me if I am wrong but you shouldn't use enums to check value ranges. Whenever you need to check values from an enum it should be inside a switch (preferably without a default case). It adds a couple of lines to your code but you will learn to love your compiler when you start adding values to an enum and the compiler tells you all the places in your source code you just forgot to adjust to the newly added value.

##### Share on other sites
Thanks for the replies.

Sorry, should have mentioned that the enums have gaps in. I don't care what the value is in this case, just whether it's in the enum. I was mainly just curious to see if there was some basic C++ thing I'd missed, but it appears not.

##### Share on other sites
There are a few things you can do to help make it obvious whether or not a particular enum belongs to a set, but there is nothing built into the language that will do it for you. For example, you could put the enum set into a namespace. Or you could embed a common name into all of the enums, such as:
enum eColors{   eColor_Red,   eColor_Green,   eColor_Blue,};

The particular notation you use doesn't matter. Some people would not like the 'e' in front of everything that is denoting it's an 'enum', some people would make them all CAPS, etc.

The point is simply to give all of the enums within the set a common base, either through the namesapce, the naming convention, or whatever, to make it obvious which set the particular enum belonged to.

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002135
×