问题
I can generate name for a function using an macro which is taken from C pre-processor defining for generated function names .
#define POSTFIX _ABC //_ABC should be mentioned only there
#define PROTOTYPENAME(symbol) symbol ## POSTFIX
#define PROTOTYPENAMED(sym) PROTOTYPENAME(sym)
int PROTOTYPENAMED(Function)(int a, int b, int c);
Above would result as
int Function_ABC(int a, int b, int c);
If I want to generate equivalent for #define ANOTHERFUNCTION_ABC WhatsThat()
I might want to write
#define PROTOTYPENAMED(ANOTHERFUNCTION) WhatsThat()
but this will redefine a PROTOTYPENAMED
macro. Is there a way to do it correctly without writing _ABC
everywhere?
PS. I am using Visual Studio 2013 and code should work in C and C++.
Edit: alk: this question contains more useful answer how to do this using another tools. You should not mark this question as dublicate.
Edit 2:
I was thinking about alternatives that do not need an external tools. Maybe it is not needed to declare it as macro? If we assume WhatsThat()
is returning int
, it is not forbidden to write int PROTOTYPENAMED(ANOTHERFUNCTION) = WhatsThat();
.
回答1:
Not doable with the standard C/C++ preprocessor. However, you might feed your compiler using some C++ or C source generator, like e.g. the GPP or the GNU m4 preprocessors, or some script -e.g. in Python, awk
, guile
, ...- (or program) of yours.
For example, yacc or bison
and ANTLR generate C++ code. And MELT is compiled into C++ code.
Of course, you need to configure your builder accordingly, e.g. add some more GNU make rules.
来源:https://stackoverflow.com/questions/28327691/generate-a-name-for-macro-with-another-macro-c-preprocessor