问题
I have the following code:
#define MY_MACRO(PARAM) int PARAM_int; double PARAM_double; [subsequent instructions]
Unfortunately, it does not work, meaning that PARAM is not replaced inside the variables names. Is this solvable some way?
回答1:
PARAM_int is considered to be a single token, that is distinct from PARAM. You can concatenate tokens in a macro definition with ##:
#define MY_MACRO(PARAM) int PARAM ## _int; double PARAM ## _double;
Now, PARAM will expand to whatever you invoke the macro with, and then the resulting token will be pasted together with _int and _double.
来源:https://stackoverflow.com/questions/27927988/in-a-define-clause-how-to-make-the-preprocessor-replace-a-parameter-inside-a-v