Re: macro for enum to string?

Discussion in 'C++' started by Paul Mensonides, Apr 1, 2004.

  1. "Bernhard Holzmayer" <> wrote in message
    news:...
    > Jerald Fijerald wrote:
    > >> #include <iostream>
    > >>
    > >> #define CREATE_ENUM(t,a,b,c) enum t { a, b, c }; \
    > >> const char* s_##t[] = { #a, #b,#c };
    > >>
    > >> CREATE_ENUM(my_enum,red,green,blue);
    > >>

    > >
    > > What if you want:
    > > enum my_enum { red=10, green=16, blue=65000 };
    > >
    > > inefficient. It'd be great if you could generate a switch ()
    > > statement code.....
    > >
    > > const char *enum_name (int i)
    > > {
    > > switch (i) {
    > > case red: return "red";
    > > case blue: return "blue";
    > > ...
    > > default: return 0;
    > > }
    > >
    > > Maybe boost has recusrive macros to do this...
    > >
    > > Gerald

    >
    > I agree, your idea strikes. I take the challenge:
    >
    > #define CREATE_ENUM(t,a,b,c) enum t { a, b, c }; \
    > const char * name_##t(int i) { \
    > switch (i) { \
    > case a: return #a; \
    > case b: return #b; \
    > case c: return #c; \
    > default: return "undefined color"; }}
    >
    >
    > and this may be called with:
    > CREATE_ENUM(red,green,blue)
    >
    > However, the discussed problem, that this macro must be defined once
    > for every number of entries.


    All of the above is possible, except that you'd need variadic macros from C99
    (which aren't yet part of C++) to give you the syntax that you desire. The best
    that you can do with current C++ is:

    CREATE_ENUM(my_enum, (red)(green)(blue))

    -or-

    CREATE_ENUM(my_enum, ((10)red)((16)green)((65000)blue))

    However, doing the last part requires a preprocessor that is relatively decent
    (unlike many popular preprocessors). There are other syntactic possibilities of
    course, but in C++ you must always stay away from anything where the arity
    changes without explicitly telling the mechanism what arity it is, which is the
    problem with:

    CREATE_ENUM(my_enum, (red, 10)(blue)(green, 65000))

    Otherwise, it is easy to construct such a macro, especially using the Boost
    pp-lib.

    Regards,
    Paul Mensonides
     
    Paul Mensonides, Apr 1, 2004
    #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. Bernhard Holzmayer

    Re: macro for enum to string?

    Bernhard Holzmayer, Apr 1, 2004, in forum: C++
    Replies:
    1
    Views:
    3,254
    Bernhard Holzmayer
    Apr 1, 2004
  2. Eric Sosman
    Replies:
    3
    Views:
    795
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    509
  4. Lew
    Replies:
    0
    Views:
    545
  5. Roedy Green
    Replies:
    0
    Views:
    741
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page