在#define宏中转义#符号? [英] Escaping a # symbol in a #define macro?

查看:175
本文介绍了在#define宏中转义#符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无需使用任何细节,我想使用一个#define宏,该宏将扩展为#include,但是'#'符号使预处理器感到困惑(因为它认为我想引用一个参数.)

例如,我想做这样的事情:

#define MACRO(name) #include "name##foo"

并按以下方式使用它:

MACRO(Test)

这将扩展为:

#include "Testfoo"

谦虚的#号导致预处理器发出响声. MinGW给我以下错误:

'#' is not followed by a macro parameter

我想我需要转义#号,但是即使有可能我也不需要.

是的,宏确实是邪恶的...

解决方案

据我所知,您不能在define中使用另一个预处理程序指令.

Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)

For example, I want to do something like this:

#define MACRO(name) #include "name##foo"

And use it thus:

MACRO(Test)

Which will expand to:

#include "Testfoo"

The humble # sign is causing the preprocessor to barf. MinGW gives me the following error:

'#' is not followed by a macro parameter

I guess I need to escape the # sign but I don't if this is even possible.

Yes, macros are indeed evil...

解决方案

As far as I remember you cannot use another preprocessor directive in define.

这篇关于在#define宏中转义#符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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