"demo_enum.h"ide
- #include "enum2string.h"
- //enum Type {
- // Foo,
- // Bar,
- // Team
- //};
- DEFINE_ENUM_WITH_STRING_CONVERSIONS(Type, (Foo)(Bar)(Team))
"enum2string.h"spa
- #include <boost/preprocessor.hpp>
- #define X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE(r, data, elem) \
- case elem : return BOOST_PP_STRINGIZE(elem);
- #define DEFINE_ENUM_WITH_STRING_CONVERSIONS(name, enumerators) \
- enum name { \
- BOOST_PP_SEQ_ENUM(enumerators) \
- }; \
- \
- inline char* ToString(name v) \
- { \
- switch (v) \
- { \
- BOOST_PP_SEQ_FOR_EACH( \
- X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE, \
- name, \
- enumerators \
- ) \
- default: return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
- } \
- }
"main.cpp"get
- #include "demo_enum.h"
- int main(int argc, char* argv[])
- {
- Type t = Team;
- char *p = ToString(t);
- char *p1 = ToString(Team);
- return 0;
- }
Reference: string
http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-stringit