Say you want to generate a matched list of identifiers and strings
enum
{
NAME_ONE,
NAME_TWO,
NAME_THREE
};
myFunction(NAME_ONE, "NAME_ONE");
myFunction(NAME_TWO, "NAME_TWO");
myFunction(NAME_THREE, "NAME_THREE");
..without repeating yourself, and without auto-generating the code, using C/C++ macros
Initial guess:
You could add an #include file containing
myDefine(NAME_ONE)
myDefine(NAME_TWO)
myDefine(NAME_THREE)
Then use it twice like:
#define myDefine(a) a,
enum {
#include "definitions"
}
#undef myDefine
#define myDefine(a) myFunc(a, "a");
#include "definitions"
#undef myDefine
but #define doesn't let you put parameters within a string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…