C:扩展宏标记粘贴 [英] C: Expand Macro With Token Pasting

查看:97
本文介绍了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_FUNCTION和MODULE_STRUCT内MODULE_NAME扩张不再发生......这似乎认为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.

有没有办法解决?

推荐答案

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆