适用于利用枚举表示类型的场景(从0开始连续递增), QT代码,标准C++环境替换一下模板类型
#define ENUM(name, ...) \
enum class name { __VA_ARGS__ }; \
inline const QStringList& name##StrList() { QStringList list = QString(#__VA_ARGS__).split(","); static QStringList res; if (res.isEmpty()) for(QString s: list) { res.append(s.trimmed()); } return res; }; \
inline const QString& name##Str(name t) { return name##StrList().at(static_cast<int>(t)); }; \
inline QDebug operator<< (QDebug d, const name& o) { d << name##Str(o); return d; };
#define ENUM_LIST(name, s, e) \
inline name& operator++(name& o) {o = static_cast<name>(static_cast<int>(o) + 1); return o; }; \
inline name operator++(name& o, int) { auto old = o; ++o; return old; }; \
inline QList<name>& name##List() { static QList<name> list; if (list.isEmpty()) for (name i=s; i<=e; ++i) { list.append(i); } return list; }; \
获取遍历列表
// enum class Demo { A, B, C };
ENUM(DEMO, A, B, C);
ENUM_LIST(DEMO, DEMO::A, DEMO::C);
QList<Demo>& list = DemoList();
QStringList& str_list = DemoStrList();
获取名称
Demo o = A;
QString& str = DemoStr(o);