Enum to String

C

Christopher

This secenario has come up several times in my career and I've seen
several ways of doing it.
Usually, I wonder why enums were used in the first place, when perhaps
some other contruct could have been easier to follow. However, given
that you have some enumeration type, how do you personally choose to
map it to a corresponding constant string?

1) I've seen a function or method at the same scope containing a large
switch
2) I've seen the enumeration ordered such that each is an index to an
array of corresponding strings
3) ...etc.
 
V

Victor Bazarov

This secenario has come up several times in my career and I've seen
several ways of doing it.
Usually, I wonder why enums were used in the first place, when perhaps
some other contruct could have been easier to follow. However, given
that you have some enumeration type, how do you personally choose to
map it to a corresponding constant string?

1) I've seen a function or method at the same scope containing a large
switch
2) I've seen the enumeration ordered such that each is an index to an
array of corresponding strings
3) ...etc.

I'd probably use a std::map<your_enum_type, std::string> and a function
(or a macro) to extract the mapped value. Works equally well for
sparsely defined enumerators and sequential ones.

V
 
A

AnonMail2005

This secenario has come up several times in my career and I've seen
several ways of doing it.
Usually, I wonder why enums were used in the first place, when perhaps
some other contruct could have been easier to follow. However, given
that you have some enumeration type, how do you personally choose to
map it to a corresponding constant string?

1) I've seen a function or method at the same scope containing a large
switch
2) I've seen the enumeration ordered such that each is an index to an
array of corresponding strings
3) ...etc.

I've used functions to go from enumerators to strings and vice-versa.
Only the functions declarations are visible in the header. For
implementation, I use static arrays of POD structs which contain the
enumerator and a const char * of the "string". The lookup is linear
but I've never used enumerators with many values so this was never a
downside. The upside is that the static array is initialize on
program load so there's no initialization logic (e.g. for a map).

I've also used template versions of the above so the code duplication
was kept to a minimum. The templates were explicitly instantiates in
the .cpp files to keep the implementation machinery out of the header
files.

HTH
 
G

Gert-Jan de Vos

This secenario has come up several times in my career and I've seen
several ways of doing it.
Usually, I wonder why enums were used in the first place, when perhaps
some other contruct could have been easier to follow. However, given
that you have some enumeration type, how do you personally choose to
map it to a corresponding constant string?

1) I've seen a function or method at the same scope containing a large
switch
2) I've seen the enumeration ordered such that each is an index to an
array of corresponding strings
3) ...etc.

I made a function template To enum_cast<To>(From) and a set of macros
that define specializations of this template for each From->To
mapping.
Mappings are typically from one enum to another equivalent enum, but
can also handle int to enum or enum to string mappings. The macros
create a simple switch/case body for the enum_cast function. default
throws bad_enum_cast. The macros can use the # operator to create
the string.

The mapping definition macros are self describing and the enum_cast<>
syntax fits in very well with C++.

Gert-Jan
 
J

Juha Nieminen

Victor Bazarov said:
I'd probably use a std::map<your_enum_type, std::string> and a function
(or a macro) to extract the mapped value. Works equally well for
sparsely defined enumerators and sequential ones.

If the enumerated values are consecutive, std::map would be overkill.
Using a static array is much more efficient and less memory-consuming
(and is easier to initialize).

Of course if you use a stongly-typed enum in C++11, then it becomes
more problematic because the enumerated names are not convertible to
integrals. (I think only a switch statement would be possible.)
 
C

Christopher

Of course if you use a stongly-typed enum in C++11, then it becomes
more problematic because the enumerated names are not convertible to
integrals. (I think only a switch statement would be possible.)

I googled up C+11 and strongly-type enums and read a number of things
that will assist with some things I've found really annoying.
We still don't have any compiler that implements C+11 entirely right?
Last I heard, VC2010 partially implements it, but not completely.
 
J

Juha Nieminen

Christopher said:
I googled up C+11 and strongly-type enums and read a number of things
that will assist with some things I've found really annoying.

In short, in C++11 you can, optionally, make an enum strongly typed
(iow. it's not implicitly convertible to integral, and the enumerated
names will be inside the scope of the enum rather than the surrounding
scope), and you can, also optionally, specify the (integral) storage type
of the enumerated values (to be eg. char or short, if you need the space
savings).
We still don't have any compiler that implements C+11 entirely right?
Last I heard, VC2010 partially implements it, but not completely.

Not entirely, but strongly typed enums are quite widely supported
already. At least gcc and clang support it (and probably a recent version
of VC++).
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top