compile-time reflection on enum types?

Discussion in 'C++' started by IR, Oct 22, 2006.

  1. IR

    IR Guest

    Hi,

    I'm trying to store values of an enum type in a std::bitset
    container, in a contiguous manner.

    eg.
    typedef enum
    {
    meValue1 = 0xbaad,
    meValue2 = 0xbeef,
    meValue3 = 0xdead,
    meValue4 = 0xbeef
    } MyEnum;

    should be stored in a std::bitset<3> (due to the duplicate value
    0xbeef).

    I don't consider using a std::set<MyEnum> instead, because I would
    then be lacking std::bitset facilities such as flip() and set() that
    work on the whole set of values.

    My goal is to provide some kind of "enumset" template container that
    would store only legal values of it's parametrized enum type.

    Of course there is the solution to manually map each enum value to a
    bit index (and the other way around too), but I'd like to automate
    this if possible (which I didn't achieve so far).

    The ideal would be to have compile-time reflection on the enum type,
    but searching on the web yielded no relevant results so I guess I'm
    in a deadend as far as a (value <=> bit index) automatic mapping is
    concerned.

    Another idea was to make the std::bitset large enough to handle
    _all_ possible values between minimum and maximum values of the
    enum. But this seems impractical with enums that have large
    differences between their min and max value.
    eg. for MyEnum : 0xbaad to 0xdead would be 9217 bits ie. 1153 bytes
    (but we really need only 1 byte, as we have 3 distinct values).

    Any suggestions are welcome, I'm kinda stuck ATM. :(

    --
    IR
     
    IR, Oct 22, 2006
    #1
    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:
    554
  2. Jerminia
    Replies:
    3
    Views:
    636
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    475
    Rolf Magnus
    Nov 13, 2003
  4. Nagaraj
    Replies:
    1
    Views:
    883
    Lionel B
    Mar 1, 2007
  5. Carter
    Replies:
    2
    Views:
    513
    Carter
    Mar 4, 2009
Loading...

Share This Page