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

Enumerated Constants

Is there an efficient way to check whether an enumerator exists when
casting an integer into an enumerated value?

For example, say I have this:

enum MyEnum {
a = 1,
b = 2,
// more arbitrary constants
c = 1000
}

Is there a way to create a function that would return true if 1 was
entered, while (assuming 3 is not the value of a constant in MyEnum)
entering 3 would return false?

--
I am only a mirage.
Jul 22 '05 #1
5 1627
"kelvSYC" <ke*****@no.email.shaw.ca> wrote...
Is there an efficient way to check whether an enumerator exists when
casting an integer into an enumerated value?
Not really.
For example, say I have this:

enum MyEnum {
a = 1,
b = 2,
// more arbitrary constants
c = 1000
}

Is there a way to create a function that would return true if 1 was
entered, while (assuming 3 is not the value of a constant in MyEnum)
entering 3 would return false?


Only by checking each value can you make sure:

bool isMyEnum(MyEnum e)
{
switch (e)
{
case a: case b: /* more case clauses */ case c: return true;
}
return false;
}

Victor
Jul 22 '05 #2
You're probably better off using a set-handling library than an enum for
this.

--
gabriel
Jul 22 '05 #3
kelvSYC wrote:
Is there an efficient way to check whether an enumerator exists when
casting an integer into an enumerated value?

For example, say I have this:

enum MyEnum {
a = 1,
b = 2,
// more arbitrary constants
c = 1000
}

Is there a way to create a function that would return true if 1 was
entered, while (assuming 3 is not the value of a constant in MyEnum)
entering 3 would return false?

There is an alternative to enums that might be interesting to you.

If you boil down to what is represented by an enum, then it is easily
represented by a class. If you take this to the extreme level, then
enums are evil and you should represent all enums with a class.
class Color; // akin to enum
extern const Color & white; // different elements in the enum
extern const Color & black; // different elements in the enum

//
// operations you want to do on the enum
//
int OrdOf( const Color & );
int IsInSet( int );

inline bool operator==( const Color & b, const Color & a )
{
return &b == &a;
}

inline bool operator<( const Color & b, const Color & a )
{
return OrdOf( b ) < OrdOf( a );
}

//
// example expressions
//

int main()
{
white == black;

IsInSet( 3 );
}
//
// in a different cpp file - implementation
//

#include <set>
#include <cassert>

class Color
{
public:
int m_ord;

static std::set<int> m_set;

Color( int ord )
: m_ord( ord )
{
m_set.insert( ord );
}

};

std::set<int> Color::m_set;

int OrdOf( const Color & i_color )
{
return i_color.m_ord;
}

int IsInSet( int i_ord )
{
return Color::m_set.find( i_ord ) != Color::m_set.end();
}

namespace
{
Color g_white( 1 );
Color g_black( 2 );
Color g_xxx( 3 ); // this forces a core dump.

};

const Color & white = g_white;
const Color & black = g_black;

Jul 22 '05 #4
Gianni Mariani wrote:
kelvSYC wrote:
Is there an efficient way to check whether an enumerator exists when
casting an integer into an enumerated value?

For example, say I have this:

enum MyEnum {
a = 1,
b = 2,
// more arbitrary constants
c = 1000
}

Is there a way to create a function that would return true if 1 was
entered, while (assuming 3 is not the value of a constant in MyEnum)
entering 3 would return false?


There is an alternative to enums that might be interesting to you.

If you boil down to what is represented by an enum, then it is easily
represented by a class. If you take this to the extreme level, then
enums are evil and you should represent all enums with a class.
class Color; // akin to enum
extern const Color & white; // different elements in the enum
extern const Color & black; // different elements in the enum

//
// operations you want to do on the enum
//
int OrdOf( const Color & );
int IsInSet( int );

inline bool operator==( const Color & b, const Color & a )
{
return &b == &a;
}


Shouldn't this be:
return b == a;

Your comparing the locations of the two. I believe you want
to compare by value. Two instances may have the same value
but reside in different locations.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #5
Thomas Matthews wrote:
Gianni Mariani wrote:
kelvSYC wrote:
Is there an efficient way to check whether an enumerator exists when
.... Shouldn't this be:
return b == a;

Your comparing the locations of the two. I believe you want
to compare by value. Two instances may have the same value
but reside in different locations.


That's dependant on the requirements. Since this is an arbitrary
example and *my* example I can make it anything I want it to be.

Nice try...

Jul 22 '05 #6

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

Similar topics

3
by: Edward Diener | last post by:
In an assembly I have a __value enum with some enumerated constants, ie. namespace X { public __value enum MyEnum { ValueA, ValueB }; }
1
by: Nick Bassiliades | last post by:
Hi everybody, I would like to ask a very specific question about how I can re-define an enumerated attribute type. Specifically, I have two XML Schema files. In file A I have the following...
3
by: Curtis Justus | last post by:
Hi, We are porting an application that contains a list of strings. We would like to use these strings within code such as a switch statement. What would be the best way to create a list of...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
7
by: John Goche | last post by:
Hello, The following program compiler and runs fine under gcc and produces the output 3. However, I am not sure whether such behavior is legal. In particular, as a related question, how would I...
1
by: senfo | last post by:
I'm using an enumerated type to identify a record type that relates to a unique ID in a database. Identity columns in SQL start at 1, while enumerated types in C# start at 0. I didn't think it...
4
by: Peted | last post by:
Hi i am trying to make a small mdoification to and exisiting dll control, in its source code. Im trying to expose one of its internal properties so the user can set it in the ide at...
11
by: David Mathog | last post by:
Is there a standard compliant method to access the number of elements in an enumerated type in C, either from within the preprocessor or the running program? The example below compiles and runs...
8
by: Christian Blackburn | last post by:
Hi Gang, I'm a long time VB programmer and in that language I now how to combine constants. Can somebody give me the correct syntax for combining constants in C# for the following dialog: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: 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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.