简体   繁体   English

在#define子句中,如何使预处理器替换变量名称中的参数?

[英]In a #define clause, how to make the preprocessor replace a parameter inside a variable name?

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. 不幸的是,它不起作用,这意味着未在变量名称内替换PARAM。 Is this solvable some way? 这可以解决吗?

PARAM_int is considered to be a single token, that is distinct from PARAM . PARAM_int被认为是单个令牌,与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 . 现在, PARAM将扩展为您使用宏调用的对象,然后将生成的令牌与_int_double粘贴在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM