C:使用令牌粘贴扩展宏 [英] C: Expand Macro With Token Pasting
本文介绍了C:使用令牌粘贴扩展宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我创建的一些宏:
So here are some macros I have created:
#define MODULE_NAME moduleName
#define MODULE_STRUCT MODULE_NAME ## _struct
#define MODULE_FUNCTION(name) MODULE_NAME ## _ ## name
在这些定义之后,我希望发生以下扩展:
After those definitions, I would like the following expansions to happen:
MODULE_STRUCT --> moduleName_struct
MODULE_FUNCTION(functionName) --> moduleName_functionName
但是,当我添加标记粘贴运算符时,MODULE_NAME 在 MODULE_FUNCTION 和 MODULE_STRUCT 中的扩展不再发生......在将它们粘贴在一起时似乎将 MODULE_NAME 视为文字字符串.
However, when I add the token pasting operators, expansion of MODULE_NAME within MODULE_FUNCTION and MODULE_STRUCT no longer happens... It seems to consider MODULE_NAME as a literal string when pasting them together.
有没有办法解决这个问题?
Is there a way around this?
推荐答案
在 C 中,标记粘贴运算符 ##
的操作数不会被扩展.
In C the operands of the token pasting operator ##
are not expanded.
您需要第二级间接来获得扩展.
You need a second level of indirection to get the expansion.
#define CAT(x, y) CAT_(x, y)
#define CAT_(x, y) x ## y
这篇关于C:使用令牌粘贴扩展宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文