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

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

    Suma's macro solution is nice. You don't need to have two different macro's, though. C++ wil happily include a header twice. Just leave out the include guard.

    So you'd have an foobar.h defining just

    ENUM(Foo, 1)
    ENUM(Bar, 2)
    

    and you would include it like this:

    #define ENUMFACTORY_ARGUMENT "foobar.h"
    #include "enumfactory.h"
    

    enumfactory.h will do 2 #include ENUMFACTORY_ARGUMENTs. In the first round, it expands ENUM like Suma's DECLARE_ENUM; in the second round ENUM works like DEFINE_ENUM.

    You can include enumfactory.h multiple times, too, as long as you pass in different #define's for ENUMFACTORY_ARGUMENT

提交回复
热议问题