Wednesday, August 23, 2017

Enum with string

If you need enums and strings that are equivalent as that names, You can declare like this.

#define ENM \
E(AAA)\
E(BBB)

#define E(x) x,
enum {
    ENM
};
#undef E

#define E(x) #x,
const char* strs[] = {
    ENM
};
#undef E

int main() {
    cout << AAA << endl;          // 0
    cout << strs[AAA] << endl;    // AAA
    return 0;
}

No comments:

Post a Comment

It&#39;s magic! std::bind

'std::bind' is a powerful helper for working around 'std::function'. 'std::bind' makes a instance of 'std::fun...