Max value of an ENUM

Discussion in 'C++' started by Andrea Crotti, Jan 30, 2011.

  1. 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.)
    Andrea Crotti, Jan 30, 2011
    #1
    1. Advertising

  2. Andrea Crotti

    Larry Evans Guest

    On 01/30/11 05:56, Andrea Crotti wrote:
    > 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
    Larry Evans, Jan 30, 2011
    #2
    1. Advertising

  3. On Jan 30, 2:56 pm, Andrea Crotti <> wrote:
    > 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
    Saeed Amrollahi, Jan 30, 2011
    #3
  4. On Jan 30, 1:33 pm, Saeed Amrollahi <> wrote:
    > On Jan 30, 2:56 pm, Andrea Crotti <> wrote:
    >
    >
    >
    > > 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


    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?
    Andrea Crotti, Jan 30, 2011
    #4
  5. 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.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 30, 2011
    #5
  6. On Jan 30, 5:35 pm, Victor Bazarov <> wrote:
    > 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?
    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...
    Andrea Crotti, Jan 30, 2011
    #6
  7. On 1/30/2011 11:47 AM, Andrea Crotti wrote:
    > On Jan 30, 5:35 pm, Victor Bazarov<> wrote:
    >> 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? Get yourself a copy of the Standard and
    read 7.2 [dcl.enum].

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 30, 2011
    #7
  8. Andrea Crotti

    Paul Guest

    "Victor Bazarov" <> wrote in message
    news:ii44vr$an6$-september.org...
    > On 1/30/2011 11:47 AM, Andrea Crotti wrote:
    >> On Jan 30, 5:35 pm, Victor Bazarov<> wrote:
    >>> 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?
    Paul, Jan 31, 2011
    #8
  9. Andrea Crotti

    Paul Guest

    "Victor Bazarov" <> wrote in message
    news:ii4404$vpa$-september.org...
    > 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.
    >

    He means the default type for the underlying integral , obviously.
    Paul, Jan 31, 2011
    #9
  10. Andrea Crotti

    Paul Guest

    "Victor Bazarov" <> wrote in message
    news:ii44vr$an6$-september.org...
    > On 1/30/2011 11:47 AM, Andrea Crotti wrote:
    >> On Jan 30, 5:35 pm, Victor Bazarov<> wrote:
    >>> 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. :)
    Paul, Jan 31, 2011
    #10
  11. Andrea Crotti

    James Kanze Guest

    On Jan 30, 4:47 pm, Andrea Crotti <> wrote:
    > On Jan 30, 5:35 pm, Victor Bazarov <> wrote:


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

    --
    James Kanze
    James Kanze, Jan 31, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    538
  2. Kevin
    Replies:
    7
    Views:
    3,141
    Andreas Huber
    Jun 23, 2007
  3. Summercool
    Replies:
    9
    Views:
    879
    dorayme
    Oct 23, 2007
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    576
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    334
    kriton
    Apr 22, 2005
Loading...

Share This Page