Max value of an ENUM

A

Andrea Crotti

So I have to handle an event and I have a function like

void handleEvent(int type)

(and I can't modify this prototype).
Now I had an ENUM for keeping the events, but it turns out that I need
to generate many more events, and have a function to generate the event
number.

So should I still keep the enum?
If yes I would need a starting point to begin with my own generated
events.
Is it possible to get the max of an ENUM?

(In theory I can just take the last of course, but suppose that I might
add many more.)
 
L

Larry Evans

So I have to handle an event and I have a function like

void handleEvent(int type)

(and I can't modify this prototype).
Now I had an ENUM for keeping the events, but it turns out that I need
to generate many more events, and have a function to generate the event
number.

So should I still keep the enum?
If yes I would need a starting point to begin with my own generated
events.
Is it possible to get the max of an ENUM?

(In theory I can just take the last of course, but suppose that I might
add many more.)
Would providing a specialization of a class template with the max
value defined as a static work? Something like:

template<typename Enum>
struct max_enum
;

enum my_enum
{ my_0
, my_1
, my_2
....
, my_n
};

template<>
struct max_enum<my_enum>
{
static
unsigned const
value=my_n;
};

Of course each time you change the # of enumerators
you'd have to change the max_enum<my_enum> specializaiton;
however, if those two are kept in the same place in the
code, it shouldn't be that much of a burden.

HTH.

-Larry
 
S

Saeed Amrollahi

So I have to handle an event and I have a function like

void handleEvent(int type)

(and I can't modify this prototype).
Now I had an ENUM for keeping the events, but it turns out that I need
to generate many more events, and have a function to generate the event
number.

So should I still keep the enum?
If yes I would need a starting point to begin with my own generated
events.
Is it possible to get the max of an ENUM?

(In theory I can just take the last of course, but suppose that I might
add many more.)

Hi

I usually use the following technique:
enum EEvent { Event0, Event1, Event2, /* ... */, MAX_EVENT };
Actually, MAX_EVENT is the number of actual enumerator.
In C++, enumeration has underlying type like short or int.
In C++0x, you can specify the underlying type explicitly:
enum EEvent : long long { Event0 = 0, Event1 = 1, MAX_EVENT =
2LL };
the default underlying type is int. I think, you don't need to worry
about
the number of events.

Regards,
-- Saeed Amrollahi
 
A

Andrea Crotti

Hi

I usually use the following technique:
     enum EEvent { Event0, Event1, Event2, /* ... */, MAX_EVENT };
Actually, MAX_EVENT is the number of actual enumerator.
In C++, enumeration has underlying type like short or int.
In C++0x, you can specify the underlying type explicitly:
     enum EEvent : long long { Event0 = 0, Event1 = 1, MAX_EVENT =
2LL };
the default underlying type is int. I think, you don't need to worry
about
the number of events.

Regards,
  -- Saeed Amrollahi

Thank you all for the quick answer.
I like this solution more, easy and working :)

I might run into overflow in theory, and even if I do I always want to
get a number greater than X.

For example I have
0..99 which are reserved

I only want to range from 100 to MAX_NUMBER, and when I overflow go
back to 100.
But if I simply do "100 + i" it doesn't work since I will get 0 at
some time, correct?

And shouldn't be the enum an unsigned type by default?
 
V

Victor Bazarov

[..]
And shouldn't be the enum an unsigned type by default?

No. Why should it? And what do you mean by "default"? There is no
such thing as "default" for user-defined types.

V
 
A

Andrea Crotti

[..]
And shouldn't be the enum an unsigned type by default?

No.  Why should it?  And what do you mean by "default"?  There is no
such thing as "default" for user-defined types.

An enumeration makes sense with an index negative?
I didn't mean in c++-0x where you can define what type you want, but
in c++ pre 0x it's an int, right? And that's not an user-defined
type...
 
V

Victor Bazarov

[..]
And shouldn't be the enum an unsigned type by default?

No. Why should it? And what do you mean by "default"? There is no
such thing as "default" for user-defined types.

An enumeration makes sense with an index negative?

What's "index"? An enumeration is a collection of named constants.
I didn't mean in c++-0x where you can define what type you want, but
in c++ pre 0x it's an int, right?

No, it most certainly isn't.
> And that's not an user-defined
type...

What isn't a user-defined type? Get yourself a copy of the Standard and
read 7.2 [dcl.enum].

V
 
P

Paul

Victor Bazarov said:
On 1/30/2011 10:27 AM, Andrea Crotti wrote:

[..]
And shouldn't be the enum an unsigned type by default?

No. Why should it? And what do you mean by "default"? There is no
such thing as "default" for user-defined types.

An enumeration makes sense with an index negative?

What's "index"? An enumeration is a collection of named constants.
I didn't mean in c++-0x where you can define what type you want, but
in c++ pre 0x it's an int, right?

No, it most certainly isn't.
And that's not an user-defined
type...

What isn't a user-defined type?

An integer isn't a user defined type.
Get yourself a copy of the Standard and read 7.2 [dcl.enum].
Why what does it say there?
 
P

Paul

Victor Bazarov said:
[..]
And shouldn't be the enum an unsigned type by default?

No. Why should it? And what do you mean by "default"? There is no such
thing as "default" for user-defined types.
He means the default type for the underlying integral , obviously.
 
P

Paul

Victor Bazarov said:
On 1/30/2011 10:27 AM, Andrea Crotti wrote:

[..]
And shouldn't be the enum an unsigned type by default?

No. Why should it? And what do you mean by "default"? There is no
such thing as "default" for user-defined types.

An enumeration makes sense with an index negative?

What's "index"? An enumeration is a collection of named constants.
I didn't mean in c++-0x where you can define what type you want, but
in c++ pre 0x it's an int, right?

No, it most certainly isn't.
An enumerators underlying type probably is an integer type most of the time.
Victor is probably wrong here, it's not uncommon. :)
 
J

James Kanze

On 1/30/2011 10:27 AM, Andrea Crotti wrote:
[..]
And shouldn't be the enum an unsigned type by default?
No. Why should it? And what do you mean by "default"? There is no
such thing as "default" for user-defined types.
An enumeration makes sense with an index negative?

The C++ keyword "enum" can be used to implement an enumeration,
but that isn't the only use. And even when it's used to
implement an enumeration, I occasionally use a special -1 value
for not set.
I didn't mean in c++-0x where you can define what type you want, but
in c++ pre 0x it's an int, right? And that's not an user-defined
type...

In all C++, enum has always been a unique type, not an int. In
C++98 and C++03, there is an implementation defined underlying
integral type for each enum, which is large enough to hold all
of the values, but not gratuitously larger than an int. But
there is no implicit convertion from this type to the enum type.

In C, an enum also has a unique type, but the enum constants
have type int, the underlying type is required to be int, and
there is an implicit conversion of int to any enum type. (IIRC,
at least.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top