粘贴令牌时意外predefined宏行为 [英] Unexpected predefined macro behaviour when pasting tokens

查看:130
本文介绍了粘贴令牌时意外predefined宏行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下(含测试GCC -E blah.c

#define UNUSED(type) type UNUSED_ ## __COUNTER__
UNUSED(char const *)
UNUSED(int)

生成:

char const * UNUSED__COUNTER__
int UNUSED__COUNTER__

我期待:

char const * UNUSED0
int UNUSED1

我试图调用另一个宏,包裹在括号内的参数都无济于事。
如果我没有粘贴标记它似乎做工精细。
在<一个href=\"http://webcache.googleusercontent.com/search?q=cache%3ahttp://gcc.gnu.org/onlinedocs/gcc-4.4.4/cpp/Common-$p$pdefined-Macros.html\"相对=nofollow>文档特别提到在标记粘贴使用<$​​ C $ C>的__ __ COUNTER 。

I've tried calling another macro, wrapping the arguments in brackets to no avail. If I don't paste the tokens it seems to work fine. The documentation specifically mentions the use of __COUNTER__ in token pasting.

我在做什么错了?

推荐答案

用gcc 4.4做实验,这个作品:

Experimenting with gcc 4.4, this works:

#define UNUSED(type) UNUSED_(type, __COUNTER__)
#define UNUSED_(type, counter) UNUSED__(type, counter)
#define UNUSED__(type, counter) type UNUSED_ ## counter
UNUSED(char const *)
UNUSED(int)

但是,如果我拿出中间体即使只有一个级别这是行不通的。

But it doesn't work if I take out even one level of intermediates.

这篇关于粘贴令牌时意外predefined宏行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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