Conversion between enum and string in C++ class header I have the following redundant-feeling design to convert between enums and strings regarding a class that stores enums. The approach doesn't scale if there are more enums and in any event, less-redundant code is also better.

Questions


If there will be more enums, would it be possible to avoid defining two explicit conversion functions per enum type and device a system where the caller sees just one (i.e. convert) or two different function names (i.e. convertto/convertfrom for all the enums, not just per enum type)? Perhaps using some kind deduction magic with auto and decltype? It looks like ambiguity sets in since only the return value can be used to separate the different functions overloads (even if done with function templates).
Is the following design of separating the conversion functions and putting them to an anonymous namespace good design (I've thought about putting the conversion functions to a file, say conversions.incl and including it)?


The idea would be make the multiple (i.e. more enums than the one presented here) conversions as implicit as possible

The conversions would be used like this:

random.cpp

string token_string = "none"; //In reality this will be externally, user, generated.
some_class_instance-&gt;set_type(enum_conversion(token_string));
token_string = enum_conversion(some_class_instance-&gt;get_type());


And to present one enum and related conversions (but there could be more):

some_class.h

class some_class
{
    public:
         enum class enum_type
         {
             none   = 0,
             type1  = 1,
             type2  = 2
         }

     void set_type(enum_type);
     enum_type get_type() const;

   private:
       enum_type type_;
};

namespace
{
    std::array&lt;std::pair&lt;std::string, some_class::enume_type&gt;, 3&gt; type_map;

    bool initialize_map()
    {
       type_map[0] = std::make_pair("none", some_class::enum_type::none);
       type_map[1] = std::make_pair("type1", some_class::enum_type::type1);
       type_map[2] = std::make_pair("type2", some_class::enum_type::type2);
    }

    bool initialization_result = initialize_map();

    some_class::enum_type enum_conversion(std::string const&amp; enum_type)
    {
        for(auto val: type_map)
        {
            if(val.first == enum_type)
            {
                return val.second;
            }
        }

        return type_map[0].second;
    }

    std::string enum_conversion(some_class::enum_type enum_type)
    {
        for(auto val: type_map)
        {
            if(val.second == enum_type)
            {
                return val.first;
            }
        }

        return type_parameter_map[0].first;
    }
}
I would use some template logic to achieve the affect in a more scalable way:

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

// This is the type that will hold all the strings.
// Each enumeration type will declare its own specialization.
// Any enum that does not have a specialization will generate a compiler error
// indicating that there is no definition of this variable (as there should be
// be no definition of a generic version).
template&lt;typename T&gt;
struct enumStrings
{
    static char const* data[];
};

// This is a utility type.
// Created automatically. Should not be used directly.
template&lt;typename T&gt;
struct enumRefHolder
{
    T&amp; enumVal;
    enumRefHolder(T&amp; enumVal): enumVal(enumVal) {}
};
template&lt;typename T&gt;
struct enumConstRefHolder
{
    T const&amp; enumVal;
    enumConstRefHolder(T const&amp; enumVal): enumVal(enumVal) {}
};

// The next two functions do the actual work of reading/writing an
// enum as a string.
template&lt;typename T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; str, enumConstRefHolder&lt;T&gt; const&amp; data)
{
   return str &lt;&lt; enumStrings&lt;T&gt;::data[data.enumVal];
}

template&lt;typename T&gt;
std::istream&amp; operator&gt;&gt;(std::istream&amp; str, enumRefHolder&lt;T&gt; const&amp; data)
{
    std::string value;
    str &gt;&gt; value;

    // These two can be made easier to read in C++11
    // using std::begin() and std::end()
    //  
    static auto begin  = std::begin(enumStrings&lt;T&gt;::data);
    static auto end    = std::end(enumStrings&lt;T&gt;::data);

    auto find   = std::find(begin, end, value);
    if (find != end)
    {   
        data.enumVal = static_cast&lt;T&gt;(std::distance(begin, find));
    }   
    return str;
}


// This is the public interface:
// use the ability of function to deduce their template type without
// being explicitly told to create the correct type of enumRefHolder&lt;T&gt;
template&lt;typename T&gt;
enumConstRefHolder&lt;T&gt;  enumToString(T const&amp; e) {return enumConstRefHolder&lt;T&gt;(e);}

template&lt;typename T&gt;
enumRefHolder&lt;T&gt;       enumFromString(T&amp; e)     {return enumRefHolder&lt;T&gt;(e);}


Then you can use it like this:

// Define Enum Like this
enum X {Hi, Lo};
// Then you just need to define their string values.
template&lt;&gt; char const* enumStrings&lt;X&gt;::data[] = {"Hi", "Lo"};

int main()
{
    X   a=Hi;

    std::cout &lt;&lt; enumToString(a) &lt;&lt; "\n";

    std::stringstream line("Lo");
    line &gt;&gt; enumFromString(a);

    std::cout &lt;&lt; "A: " &lt;&lt; a &lt;&lt; " : " &lt;&lt; enumToString(a) &lt;&lt; "\n";
}