为另一个宏生成名称(C预处理程序) [英] generate a name for macro with another macro (c preprocessor)

查看:47
本文介绍了为另一个宏生成名称(C预处理程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用来自 C预处理程序定义生成的函数名称.

#define POSTFIX _ABC //_ABC should be mentioned only there
#define PROTOTYPENAME(symbol) symbol ## POSTFIX
#define PROTOTYPENAMED(sym) PROTOTYPENAME(sym)

int PROTOTYPENAMED(Function)(int a, int b, int c);

上面的结果为

int Function_ABC(int a, int b, int c);

如果我想生成等效于 #define ANOTHERFUNCTION_ABC WhatsThat()的代码,我可能想写

If I want to generate equivalent for #define ANOTHERFUNCTION_ABC WhatsThat() I might want to write

#define PROTOTYPENAMED(ANOTHERFUNCTION) WhatsThat()

,但这将重新定义 PROTOTYPENAMED 宏.有没有一种方法可以正确执行此操作而无需在任何地方编写 _ABC ?

but this will redefine a PROTOTYPENAMED macro. Is there a way to do it correctly without writing _ABC everywhere?

PS.我正在使用Visual Studio 2013,并且代码应在C和C ++中工作.

PS. I am using Visual Studio 2013 and code should work in C and C++.

alk:这个问题包含更有用的答案,如何使用其他工具来做到这一点.您不应将此问题标记为重复.

alk: this question contains more useful answer how to do this using another tools. You should not mark this question as dublicate.

我在考虑不需要外部工具的替代方案.也许不需要将其声明为宏?如果我们假设 WhatsThat()返回 int ,则不禁止写入 int PROTOTYPENAMED(ANOTHERFUNCTION)= WhatsThat(); .

Edit 2: I was thinking about alternatives that do not need an external tools. Maybe it is not needed to declare it as macro? If we assume WhatsThat() is returning int, it is not forbidden to write int PROTOTYPENAMED(ANOTHERFUNCTION) = WhatsThat();.

推荐答案

不适用于标准C/C ++预处理器.但是,您可以使用某些C ++或C源代码生成器(例如,例如 GPP

Not doable with the standard C/C++ preprocessor. However, you might feed your compiler using some C++ or C source generator, like e.g. the GPP or the GNU m4 preprocessors, or some script -e.g. in Python, awk, guile, ...- (or program) of yours.

例如, yacc 野牛 ANTLR 生成C ++代码.并且 MELT 被编译为C ++代码.

For example, yacc or bison and ANTLR generate C++ code. And MELT is compiled into C++ code.

当然,您需要相应地配置构建器,例如添加更多 GNU make 规则.

Of course, you need to configure your builder accordingly, e.g. add some more GNU make rules.

这篇关于为另一个宏生成名称(C预处理程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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