Macro-List-Trick
Zur Navigation springen
Zur Suche springen
Spare unnötige Code-Duplikationen bei Strings die identisch sind mit Variablennamen.
#include <iostream>
#define MEMBER_LIST(OP) \
OP(Doc) \
OP(Grumpy) \
OP(Happy) \
OP(Sleepy) \
OP(Bashful) \
OP(Sneezy) \
OP(Dopey)
int main(int argc, const char * argv[])
{
// string Doc = "Doc"; ...
#define CREATE_STRING(name) std::string name = #name;
MEMBER_LIST(CREATE_STRING)
#undef CREATE_STRING
#define LOG_STRING(name) std::cout << "string " << #name << " = \"" << name << "\"" << std::endl;
MEMBER_LIST(LOG_STRING)
#undef LOG_STRING
return 0;
}