Enum with String Value in C++: How to Map

How-to basics · Updated November 23, 2026

In C++, an enum cannot directly store a string value—its underlying type is an integer. To associate strings with enum members, you must create a separate mapping, typically using a std::map, std::unordered_map, or a switch statement. The most common and type-safe approach is to define an enum and then write a function that returns the corresponding string for each enumerator.

Why Enums Are Integers by Default

C++ enums (both unscoped and scoped enum class) are designed as a set of named integer constants. The compiler assigns an integer value to each enumerator, starting at 0 by default (unless you specify otherwise). This integer nature makes enums fast and memory-efficient, but it means they have no built-in string representation. For example:

enum Color { Red, Green, Blue };

Here, Red is actually 0, Green is 1, and Blue is 2. When you print an enum, you get the integer, not the name.

Simple String Mapping with a Function

The most straightforward way to get a string from an enum is to write a function that uses a switch or an if-else chain. This is explicit, easy to read, and works with all C++ versions.

enum class Color { Red, Green, Blue };

const char* colorToString(Color c) {
    switch (c) {
        case Color::Red:   return "Red";
        case Color::Green: return "Green";
        case Color::Blue:  return "Blue";
        default:           return "Unknown";
    }
}

Usage:

Color myColor = Color::Green;
std::cout << colorToString(myColor); // prints "Green"

This approach is simple, but if you add a new enumerator, you must remember to update the function. For small enums, it’s perfectly fine.

Using std::map or std::unordered_map for Bidirectional Lookup

If you need to convert both ways (enum to string and string to enum), a std::map or std::unordered_map is convenient. You can define a static map that pairs each enum value with its string.

#include <map>
#include <string>

enum class Color { Red, Green, Blue };

const std::map<Color, std::string> colorMap = {
    {Color::Red, "Red"},
    {Color::Green, "Green"},
    {Color::Blue, "Blue"}
};

std::string colorToString(Color c) {
    auto it = colorMap.find(c);
    return (it != colorMap.end()) ? it->second : "Unknown";
}

For the reverse (string to enum), you can build a second map or use a linear search. Note that std::map requires the enum to have a operator< (which it does by default). If you use std::unordered_map, you need to provide a hash function for the enum, which is trivial to write.

Scoped vs Unscoped Enums and String Conversion

Scoped enums (enum class) are strongly typed: they do not implicitly convert to integers, and you must qualify enumerators with the enum name. This is generally recommended because it avoids name collisions and accidental conversions. Unscoped enums (enum) are less strict; they can be implicitly converted to integers, which sometimes makes string mapping easier if you use arrays. However, scoped enums are safer and the mapping functions work identically.

Using a Macro or X-Macro for Automatic Mapping

For larger enums, writing manual switch cases or map entries is repetitive. An X-macro technique lets you define the enum and the string list in one place. You define a macro that lists all enumerators, then expand it in different contexts.

#define COLOR_LIST \
    X(Red) \
    X(Green) \
    X(Blue)

enum class Color {
    #define X(name) name,
    COLOR_LIST
    #undef X
};

const char* colorToString(Color c) {
    switch (c) {
        #define X(name) case Color::name: return #name;
        COLOR_LIST
        #undef X
        default: return "Unknown";
    }
}

This reduces duplication, but it can be harder to debug and understand for beginners. Use it only when the enum is large and changes frequently.

Common Pitfalls and How to Avoid Them

  • Forgetting to update the mapping when adding a new enumerator—always add a default case or a fallback to catch missing entries.
  • Using std::map with a non-comparable enum—unscoped enums work, but scoped enums need a custom comparator if you want a different order; the default operator< works fine.
  • Returning a dangling pointer if you return a const char* from a temporary string—prefer returning std::string by value to avoid lifetime issues.
  • Mixing up enum and enum class in a switch—for scoped enums, you must use Color::Red in case labels; otherwise, the code won’t compile.

Alternative: Using std::string_view (C++17)

If you’re using C++17 or later, you can use std::string_view to avoid copying strings. This is useful when you only need to read the string and don’t need to modify it. The mapping function can return a std::string_view that points to a static string literal.

#include <string_view>

enum class Color { Red, Green, Blue };

std::string_view colorToString(Color c) {
    switch (c) {
        case Color::Red:   return "Red";
        case Color::Green: return "Green";
        case Color::Blue:  return "Blue";
        default:           return "Unknown";
    }
}

This is efficient because no heap allocation occurs. However, be careful: the returned string_view must not outlive the string literal (which is fine here because string literals have static storage duration).

Frequently Asked Questions

Can I store a string directly in an enum?

No. In C++, an enum’s underlying type must be an integral type (int, char, etc.). You cannot have an enumerator with a string value. The standard way is to map enum values to strings externally using a function or a container.

How do I convert a string to an enum value?

You need a reverse mapping. You can iterate over a map or use a series of if statements comparing the string. For example, with a std::map<std::string, Color>, you can look up the string and get the enum. If the string is not found, you should handle the error (e.g., throw an exception or return a default).

Is there a built-in way to print an enum as a string?

No, C++ does not provide reflection or a built-in to_string for enums. You must write your own conversion function. Some libraries (like Magic Enum) offer compile-time reflection, but they are not part of the standard library.

What is the best approach for a large enum with many values?

For large enums, consider using an X-macro or a code generator to avoid duplication. Alternatively, you can use a std::unordered_map with a custom hash for faster lookup, but the switch statement is often the fastest at runtime because it can be optimized into a jump table.

Does enum class affect string conversion?

No, the conversion logic is the same. The only difference is that you must qualify the enumerators with the enum name (e.g., Color::Red) and you cannot implicitly convert to an integer. This is actually beneficial because it prevents accidental misuse.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.