Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: macro for enum to string?

Reply
Thread Tools

Re: macro for enum to string?

 
 
Bernhard Holzmayer
Guest
Posts: n/a
 
      04-01-2004
Jeff Schwab wrote:

> Bernhard Holzmayer wrote:
>
>> #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)

>
> What happened to t?

Got lost by copy&pasting the wrong line...sorry.
>
>> However, the discussed problem, that this macro must be defined
>> once for every number of entries.

>
> It would be nice to be able to specify arbitrary values for the
> enumerators, too.

Feel free to modify the macro - there's no copyright on it.

Bernhard


 
Reply With Quote
 
 
 
 
Bernhard Holzmayer
Guest
Posts: n/a
 
      04-01-2004
Bernhard Holzmayer wrote:

> Jeff Schwab wrote:
>
>> Bernhard Holzmayer wrote:
>>
>>> #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(t,red,green,blue)

>>


Just for completeness: a version with init values

#define CREATE_ENUM(t,a,ai,b,bi,c,ci) \
enum t { a=ai, b=bi, c=ci }; \
const char * name_##t(int i) { \
switch (i) { \
case a: return #a "=" #ai; \
case b: return #b "=" #bi; \
case c: return #c "=" #ci; \
default: return "undefined color"; }}


and this may be called with:

CREATE_ENUM(t,red,1 ,blue,2 ,green,3)


Bernhard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments