Is there a simple way to convert C++ enum to string?

后端 未结 30 2289
我在风中等你
我在风中等你 2020-11-22 10:37

Suppose we have some named enums:

enum MyEnum {
      FOO,
      BAR = 0x50
};

What I googled for is a script (any language) that scans all

30条回答
  •  太阳男子
    2020-11-22 11:06

    #include 
    #include 
    #define IDMAP(x) (x,#x)
    
    std::map enToStr;
    class mapEnumtoString
    {
    public:
        mapEnumtoString(){  }
        mapEnumtoString& operator()(int i,std::string str)
        {
            enToStr[i] = str;
            return *this;
        }
    public:
       std::string operator [] (int i)
        {
            return enToStr[i];
        }
    
    };
    mapEnumtoString k;
    mapEnumtoString& init()
    {
        return k;
    }
    
    int main()
    {
    
    init()
        IDMAP(1)
        IDMAP(2)
        IDMAP(3)
        IDMAP(4)
        IDMAP(5);
    std::cout<

提交回复
热议问题