宏元编程 [英] Macro Meta Programming

查看:234
本文介绍了宏元编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是要么坏的或不可能的,但因为这不是一个递归宏,我认为它应该是可能的。

I know this is probably either bad or impossible, but since this isn't a recursive macro I think it should be possible.

#define FOO 15
#define MAKE_BAR(x) BAR_##x
#define MY_FOO_BAR MAKE_BAR(FOO)

我想MY_FOO_BAR,以评价BAR_15。有没有办法告诉preprocessor传递入MAKE_BAR之前评估FOO?

I'd like MY_FOO_BAR to evaluate to BAR_15. Is there a way to tell the preprocessor to evaluate FOO before passing it into MAKE_BAR?

推荐答案

您需要宏调用另一个层面:

You need another level of macro calls:

#define FOO 15
#define MAKE_BAR_INNER(x) BAR_##x
#define MAKE_BAR(x) MAKE_BAR_INNER(x)
#define MY_FOO_BAR MAKE_BAR(FOO)

这是因为参数是如何在功能宏扩展处理。在 ## 连接符prevents参数扩展,所以必须通过添加另一个层的力的扩张。

This is because of how parameters are handled during functional macro expansion. The ## concatenation operator prevents parameter expansion, so you must "force" expansion by adding another "layer".

这篇关于宏元编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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