莫回无
这是一个基于Boost.Preprocessor的示例:#include <iostream>#include <boost/preprocessor/punctuation/comma.hpp>#include <boost/preprocessor/control/iif.hpp>#include <boost/preprocessor/comparison/equal.hpp>#include <boost/preprocessor/stringize.hpp>#include <boost/preprocessor/seq/for_each.hpp>#include <boost/preprocessor/seq/size.hpp>#include <boost/preprocessor/seq/seq.hpp>#define DEFINE_ENUM(name, values) \ enum name { \ BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_VALUE, , values) \ }; \ inline const char* format_##name(name val) { \ switch (val) { \ BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_FORMAT, , values) \ default: \ return 0; \ } \ }#define DEFINE_ENUM_VALUE(r, data, elem) \ BOOST_PP_SEQ_HEAD(elem) \ BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 2), \ = BOOST_PP_SEQ_TAIL(elem), ) \ BOOST_PP_COMMA()#define DEFINE_ENUM_FORMAT(r, data, elem) \ case BOOST_PP_SEQ_HEAD(elem): \ return BOOST_PP_STRINGIZE(BOOST_PP_SEQ_HEAD(elem));DEFINE_ENUM(Errors, ((ErrorA)(0)) ((ErrorB)) ((ErrorC)))int main() { std::cout << format_Errors(ErrorB) << std::endl;}