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

后端 未结 30 2813
我在风中等你
我在风中等你 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 10:52

    You could use a reflection library, like Ponder. You register the enums and then you can convert them back and forth with the API.

    enum class MyEnum
    {
        Zero = 0,
        One  = 1,
        Two  = 2
    };
    
    ponder::Enum::declare()
        .value("Zero", MyEnum::Zero)
        .value("One",  MyEnum::One)
        .value("Two",  MyEnum::Two);
    
    ponder::EnumObject zero(MyEnum::Zero);
    
    zero.name(); // -> "Zero"
    

提交回复
热议问题