Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++

Enum to String and Vice Versa in C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
2 Sep 2009MIT4 min read 105.8K   2.2K   30   10
A simple method to convert a C++ enumeration to its equivalent string representation and vice versa

Introduction  

While adding Serialization support to my project, I realized that I would require some way to convert a string to an enumerator and vice versa. So I did a Google search for the same, and found a lot of information; different ways in which people implemented this functionality.

However, all the solutions I found suffered from one or more of the following:

  • No support for enumerators with non-contiguous values
  • No support (not even partial support) for enumerators with duplicate values
  • No support for existing enumerations (without modifying their source code)
  • Requires one or more extra files per enumeration
  • Requires source-code to be pre-processed (by a custom binary) before compilation
  • Difficult to use or maintain
  • Highly susceptible to typos
  • Is platform/compiler specific (not portable)

So (the rip-off that I am), I borrowed the good ideas from all the solutions I found, added a few of my own and mixed-and-matched to create the code which accompanies this article. I don't claim this to be the best solution for every case, just that it solved my problem nicely and that it could be of use to someone else as well.

How To Use the Code

Using the code is quite easy. All we need to do is add one file: EnumString.h (see the source code accompanying this article) to our project.

Let's say we wanted to create an enum to represent one of the Furious Five Masters (Kung Fu Panda (2008) anyone?). So we go ahead and declare it as usual:

C++
// Furious Five Master
enum Master
{
    Tigress = 5,
    Viper = 3,
    Monkey = 4,
    Mantis = 1,
    Crane = 2
};

Don't worry about the values assigned, they are simply my rough idea of what the 'mass' of each master is, in some imaginary units. Now to add stringizing support, we need to declare the enum again, but in a different format (uses helper macros):

C++
// String support for Furious Five Master
Begin_Enum_String( Master )
{
    Enum_String( Tigress );
    Enum_String( Viper );
    Enum_String( Monkey );
    Enum_String( Mantis );
    Enum_String( Crane );
}
End_Enum_String;

And we're done! Note that since this second declaration lies in the same header/source file as the actual enum definition (probably declared just below it), it's not that difficult to update it whenever we modify the actual enumeration.

Now we can convert from a string to a Master enumerator and vice versa, quite easily. The following code shows how to do that:

C++
// Convert from a Master enumerator to a string
const std::string &masterStr = EnumString<Master>::From( Monkey );
assert( masterStr.compare( "Monkey" ) == 0 );

// Convert from a string to a Master enumerator
Master master = Tigress;
const bool bResult = EnumString<Master>::To( master, masterStr );
assert( bResult == true );
assert( master == Monkey );

Using the Code with Existing Enumerations

Suppose we want to add stringizing support to an existing enumeration from a library, which is namespaced. Imagine that the enum is declared like this in the library:

C++
namespace SomeLibrary
{
    enum WeekEnd
    {
        Sunday = 1,
        Saturday = 7
    };
}

Say that we can't modify the library files (which is anyway not a good practice). So we create a separate header file in our project, in which we will declare stringizing support for the required library enum. For the declaration, we have 3 options:

Option 1 - Fully Qualify All the Names

C++
Begin_Enum_String( SomeLibrary::WeekEnd )
{
    Enum_String( SomeLibrary::Sunday );
    Enum_String( SomeLibrary::Saturday );
}
End_Enum_String;

The consequence of this is that the stringized enums will also be fully qualified names. So, the statement EnumString<WeekEnd>::From( SomeLibrary::Saturday ) will yield "SomeLibrary::Saturday", and not just "Saturday".

Option 2 - Use the 'using namespace' Directive

C++
using namespace SomeLibrary;

Begin_Enum_String( WeekEnd )
{
    Enum_String( Sunday );
    Enum_String( Saturday );
}
End_Enum_String;

Option 3 - Register the Enumerators Yourself

Without using the 'Enum_String' helper macro (see the next section 'How does it actually work?', for an explanation of this):

C++
Begin_Enum_String( SomeLibrary::WeekEnd )
{
    RegisterEnumerator( SomeLibrary::Sunday, "Sunday" );
    RegisterEnumerator( SomeLibrary::Saturday, "Saturday" );
}
End_Enum_String;

How Does It Actually Work?

It's not required to know how it works in order to use it, so those who are not really interested can skip this section. Also, beginners might have to brush up on their C++ before reading this.

If you look at the declarations of the helper macros, you'll see that the definition of the string support for FuriousFiveMaster, works out to the following:

C++
template <> struct EnumString<Master> :
    public EnumStringBase< EnumString<Master>, Master >
{
    static void RegisterEnumerators()
    {
        RegisterEnumerator( Tigress, "Tigress" );
        RegisterEnumerator( Viper,   "Viper" );
        RegisterEnumerator( Monkey,  "Monkey" );
        RegisterEnumerator( Mantis,  "Mantis" );
        RegisterEnumerator( Crane,   "Crane" );
    }
}

You might have already realized that the above is a specialization of the EnumString template class. It defines the RegisterEnumerators function which is used by its base class EnumStringBase, via CRTP. Now the workings of the usage become clear. When you use the functions EnumString<Master>::From or EnumString<Master>::To, you're using the version of the EnumString template class which is specialized with the Master enumeration.

Drawbacks

As with most things, the code does have some drawbacks. The two most important ones are:

  • Doesn't support conversion of enumerators with duplicate values to strings (although vice versa works just fine). An attempt to convert such an enumerator will yield an empty string (which can be tested for).
  • Conversion performance might be a bottleneck for some applications. A single std::map is used internally for storing the relationship of enumerators to their string representations. So lookups during conversions are not in constant time. A conversion from an enumerator to a string will be in linear time, although a vice versa conversion should be very fast.

Closing 

Since the code makes extensive use of templates, it may not work with older compilers. The code has been tested with the following compilers:

  • Microsoft Visual C++ 2005/2008 
  • GCC 4.4.0 

There is much potential for improvement of the code. But since the current code is good enough for my needs, I'll leave that for someone else to do. If you make changes to the code, improve it, or simply have an entirely different and better way to solve this, please share it with me also; I might just dump my code and use yours instead! :)

References

  1. http://www.gamedev.net/community/forums/topic.asp?topic_id=351014
  2. http://www.gamedev.net/community/forums/topic.asp?topic_id=501798
  3. http://www.gamedev.net/community/forums/topic.asp?topic_id=437852
  4. http://www.edm2.com/0405/enumeration.html
  5. C___enums_to_strings.aspx
  6. http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4001

History

  • 3rd September, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United States United States
Besides loving spending time with family, Francis Xavier likes to watch sci-fi/fantasy/action/drama movies, listen to music, and play video-games. After being exposed to a few video-games, he developed an interest in computer programming. He currently holds a Bachelor's degree in Computer Applications.

Comments and Discussions

 
QuestionAwesome - saved me a ton of work!! Pin
Gery Gross30-Apr-19 17:12
Gery Gross30-Apr-19 17:12 
GeneralMy vote of 5 Pin
Nagata Hiroyuki14-Aug-13 5:59
Nagata Hiroyuki14-Aug-13 5:59 
QuestionMy vote of 5 Pin
Chaitanya Joshi14-Aug-13 3:02
Chaitanya Joshi14-Aug-13 3:02 
AnswerRe: My vote of 5 Pin
Francis Xavier Pulikotil19-Aug-13 1:12
Francis Xavier Pulikotil19-Aug-13 1:12 
SuggestionWhat about returning more info for unknown? Pin
John Slagel23-Jan-12 10:31
John Slagel23-Jan-12 10:31 
GeneralRe: What about returning more info for unknown? Pin
Francis Xavier Pulikotil19-Aug-13 0:28
Francis Xavier Pulikotil19-Aug-13 0:28 
GeneralTip: Alternative solution if you only want Enum to String Pin
CKLam9-Mar-10 2:39
CKLam9-Mar-10 2:39 
GeneralRe: Tip: Alternative solution if you only want Enum to String Pin
Robin23-Jun-10 0:00
Robin23-Jun-10 0:00 
GeneralThanks! This is elegant solution Pin
Dmitry Shasholko29-Sep-09 5:06
Dmitry Shasholko29-Sep-09 5:06 
GeneralVery useful and neat idea. Pin
xliqz3-Sep-09 0:15
xliqz3-Sep-09 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.