Automatic C++ enum to string mapping macro I have long been looking for an elegant, one-line solution to map enum to strings automatically, for use with the &lt;&lt; and &gt;&gt; stream operators.

I know that a lot of macros have been proposed to achieve that, but I never found a really simple scheme, with only one macro call.

This is my attempt, and I would be interested to discuss the advantages and limitations of this approach...

Definitions:

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;

#define MAKE_STRING(str, ...) #str, MAKE_STRING1_(__VA_ARGS__)
#define MAKE_STRING1_(str, ...) #str, MAKE_STRING2_(__VA_ARGS__)
#define MAKE_STRING2_(str, ...) #str, MAKE_STRING3_(__VA_ARGS__)
#define MAKE_STRING3_(str, ...) #str, MAKE_STRING4_(__VA_ARGS__)
#define MAKE_STRING4_(str, ...) #str, MAKE_STRING5_(__VA_ARGS__)
#define MAKE_STRING5_(str, ...) #str, MAKE_STRING6_(__VA_ARGS__)
#define MAKE_STRING6_(str, ...) #str, MAKE_STRING7_(__VA_ARGS__)
#define MAKE_STRING7_(str, ...) #str, MAKE_STRING8_(__VA_ARGS__)
#define MAKE_STRING8_(str, ...) #str, MAKE_STRING9_(__VA_ARGS__)
#define MAKE_STRING9_(str, ...) #str, MAKE_STRING10_(__VA_ARGS__)
#define MAKE_STRING10_(str) #str

#define MAKE_ENUM(name, ...) MAKE_ENUM_(, name, __VA_ARGS__)
#define MAKE_CLASS_ENUM(name, ...) MAKE_ENUM_(friend, name, __VA_ARGS__)

#define MAKE_ENUM_(attribute, name, ...) name { __VA_ARGS__ }; \
    attribute std::istream&amp; operator&gt;&gt;(std::istream&amp; is, name&amp; e) { \
        const char* name##Str[] = { MAKE_STRING(__VA_ARGS__) }; \
        std::string str; \
        std::istream&amp; r = is &gt;&gt; str; \
        const size_t len = sizeof(name##Str)/sizeof(name##Str[0]); \
        const std::vector&lt;std::string&gt; enumStr(name##Str, name##Str + len); \
        const std::vector&lt;std::string&gt;::const_iterator it = std::find(enumStr.begin(), enumStr.end(), str); \
        if (it != enumStr.end())\
            e = name(it - enumStr.begin()); \
        else \
            throw std::runtime_error("Value \"" + str + "\" is not part of enum "#name); \
        return r; \
    }; \
    attribute std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const name&amp; e) { \
        const char* name##Str[] = { MAKE_STRING(__VA_ARGS__) }; \
        return (os &lt;&lt; name##Str[e]); \
    }


Usage:

enum MAKE_ENUM(Test3, Item13, Item23, Item33, Itdsdgem43);

class Essai {
public:
    enum MAKE_CLASS_ENUM(Test, Item1, Item2, Item3, Itdsdgem4);

};

int main() {
    std::cout &lt;&lt; Essai::Item1 &lt;&lt; std::endl;

    Essai::Test ddd = Essai::Item1;
    std::cout &lt;&lt; ddd &lt;&lt; std::endl;

    std::istringstream strm("Item2");
    strm &gt;&gt; ddd;

    std::cout &lt;&lt; (int) ddd &lt;&lt; std::endl;
    std::cout &lt;&lt; ddd &lt;&lt; std::endl;
}
I agree with the comments: macros are a mess. Avoid them when possible.

If you're willing to make certain sacrifices (a little repetition), it's possible to avoid macros here. Doing so will also avoid some of the arbitrary limitations in your macro implementation, such as the 10 or so cap on enum values.

Using C++11, it's not hard to wire up Loki Astari's approach to operator&lt;&lt; and operator&gt;&gt; with some type traits template metaprogramming. I personally think enums are much cleaner with the strongly typed enum class ... variant introduced by C++11, so I would suggest that anyway, but the enum_serializable approach I show here works on both strongly and weakly typed enums.

#include &lt;type_traits&gt;
#include &lt;iostream&gt;
#include &lt;https://codereview.stackexchange.com/a/14315/507 by reference&gt;

template &lt;typename Enum&gt;
struct enum_serializable : std::false_type
{};

// enable operator&lt;&lt; and operator&gt;&gt; for `Essai` values
template &lt;&gt;
struct enum_serializable&lt;Essai&gt; : std::true_type
{};

template &lt;typename Enum&gt;
typename std::enable_if&lt;enum_serializable&lt;Enum&gt;::value, std::ostream&amp;
&gt;::type operator&lt;&lt;(std::ostream&amp; os, Enum e)
{
    return os &lt;&lt; enumToString(e);
}

template &lt;typename Enum&gt;
typename std::enable_if&lt;enum_serializable&lt;Enum&gt;::value, std::istream&amp;
&gt;::type operator&gt;&gt;(std::istream&amp; is, Enum&amp; e)
{
    return is &gt;&gt; enumFromString(e);
}


It's also not hard to pull Loki Astari's approach into the operator&lt;&lt; and operator&gt;&gt; functions above, removing the separate enumToString and enumFromString functions and the enumRefHolder and enumConstRefHolder structs entirely.

This general approach should be available before C++11 as well, but std::enable_if will have to be implemented instead of #included.