Skip to content

Instantly share code, notes, and snippets.

@aqzlpm11
Last active July 11, 2017 11:55
Show Gist options
  • Save aqzlpm11/12dbb734a1b70b2fdd1205a80b7fd721 to your computer and use it in GitHub Desktop.
Save aqzlpm11/12dbb734a1b70b2fdd1205a80b7fd721 to your computer and use it in GitHub Desktop.
C++: string enum
/////////// self defined ENUM_STR, have a string val
#define ENUM_STR_BEGIN(type_name) \
class type_name { \
std::string v; \
public: \
static std::set<std::string>* vals() { static std::set<std::string> s; return &s; }\
type_name(const std::string& a): v(a){} \
std::string toString() { return v; } \
bool isValid() { return vals()->find(v) != vals()->end(); }
#define ENUM_STR_VAR(name) \
class run_cmd_##name {public: run_cmd_##name(){ \
vals()->insert(#name); \
}} tmpvar##name; \
bool is##name() { return v == #name; } \
static std::string name() { return #name; }
#define ENUM_STR_END(type_name) }
/////////// ENUM_STRING END
@aqzlpm11
Copy link
Author

在C++中,用Trick,定义了一个string类型的enum。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment