Connecting Tech Pros Worldwide Forums | Help | Site Map

Enumerated Constants

kelvSYC
Guest
 
Posts: n/a
#1: Jul 22 '05
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.

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Enumerated Constants


"kelvSYC" <kelvSYC@no.email.shaw.ca> wrote...[color=blue]
> Is there an efficient way to check whether an enumerator exists when
> casting an integer into an enumerated value?[/color]

Not really.
[color=blue]
> 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?[/color]

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


gabriel
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Enumerated Constants


You're probably better off using a set-handling library than an enum for
this.

--
gabriel
Gianni Mariani
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Enumerated Constants


kelvSYC wrote:[color=blue]
> 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?[/color]


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;

Thomas Matthews
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Enumerated Constants


Gianni Mariani wrote:[color=blue]
> kelvSYC wrote:
>[color=green]
>> 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?[/color]
>
>
>
> 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;
> }[/color]

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

Gianni Mariani
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Enumerated Constants


Thomas Matthews wrote:[color=blue]
> Gianni Mariani wrote:
>[color=green]
>> kelvSYC wrote:
>>[color=darkred]
>>> Is there an efficient way to check whether an enumerator exists when[/color][/color][/color]
....[color=blue]
> 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.[/color]

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...

Closed Thread