enum to string in modern C++11 / C++14 / C++17 and future C++20

后端 未结 28 2253
逝去的感伤
逝去的感伤 2020-11-22 16:57

Contrary to all other similar questions, this question is about using the new C++ features.

  • 2008 c Is there a simple way to convert C++ enum to string?
  • <
28条回答
  •  深忆病人
    2020-11-22 17:26

    my solution is without macro usage.

    advantages:

    • you see exactly what you do
    • access is with hash maps, so good for many valued enums
    • no need to consider order or non-consecutive values
    • both enum to string and string to enum translation, while added enum value must be added in one additional place only

    disadvantages:

    • you need to replicate all the enums values as text
    • access in hash map must consider string case
    • maintenance if adding values is painful - must add in both enum and direct translate map

    so... until the day that C++ implements the C# Enum.Parse functionality, I will be stuck with this:

                #include 
    
                enum class Language
                { unknown, 
                    Chinese, 
                    English, 
                    French, 
                    German
                    // etc etc
                };
    
                class Enumerations
                {
                public:
                    static void fnInit(void);
    
                    static std::unordered_map  m_Language;
                    static std::unordered_map  m_invLanguage;
    
                private:
                    static void fnClear();
                    static void fnSetValues(void);
                    static void fnInvertValues(void);
    
                    static bool m_init_done;
                };
    
                std::unordered_map  Enumerations::m_Language = std::unordered_map ();
                std::unordered_map  Enumerations::m_invLanguage = std::unordered_map ();
    
                void Enumerations::fnInit()
                {
                    fnClear();
                    fnSetValues();
                    fnInvertValues();
                }
    
                void Enumerations::fnClear()
                {
                    m_Language.clear();
                    m_invLanguage.clear();
                }
    
                void Enumerations::fnSetValues(void)
                {   
                    m_Language[L"unknown"] = Language::unknown;
                    m_Language[L"Chinese"] = Language::Chinese;
                    m_Language[L"English"] = Language::English;
                    m_Language[L"French"] = Language::French;
                    m_Language[L"German"] = Language::German;
                    // and more etc etc
                }
    
                void Enumerations::fnInvertValues(void)
                {
                    for (auto it = m_Language.begin(); it != m_Language.end(); it++)
                    {
                        m_invLanguage[it->second] = it->first;
                    }
                }
    
                // usage -
                //Language aLanguage = Language::English;
                //wstring sLanguage = Enumerations::m_invLanguage[aLanguage];
    
                //wstring sLanguage = L"French" ;
                //Language aLanguage = Enumerations::m_Language[sLanguage];
    

提交回复
热议问题