In a #define clause, how to make the preprocessor

2020-04-19 05:48发布

问题:

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.