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

  2. Andrea Crotti

    Larry Evans Guest

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

  3. 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. 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. No. Why should it? And what do you mean by "default"? There is no
    such thing as "default" for user-defined types.

    V
     
    Victor Bazarov, Jan 30, 2011
    #5
  6. 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. What's "index"? An enumeration is a collection of named constants.
    No, it most certainly isn't.
    What isn't a user-defined type? Get yourself a copy of the Standard and
    read 7.2 [dcl.enum].

    V
     
    Victor Bazarov, Jan 30, 2011
    #7
  8. Andrea Crotti

    Paul Guest

    An integer isn't a user defined type.
    Why what does it say there?
     
    Paul, Jan 31, 2011
    #8
  9. Andrea Crotti

    Paul Guest

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

    Paul Guest

    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

    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.
    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, Jan 31, 2011
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.