C预处理器标记化不扩展宏吗? [英] C preprocessor tokenization does not expand macro?

查看:63
本文介绍了C预处理器标记化不扩展宏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)为什么在以下表达式中不扩展宏MSG?

1) Why is the macro MSG not expanded in the following expression?

#define MSG Hello
#define HELLO(name)  MSG ## name

void HELLO(Dave) () {}

使用

gcc -E -P test.cpp 

输出:

void MSGDave () {}

MSG名称扩展为 Hello Dave 。并且 MSG#名称扩展为 Hello Dave 。那么是什么原因导致gcc无法扩展 MSG ##名称

MSG name expands to Hello Dave. And MSG # name expands to Hello "Dave". So what causes gcc not to expand MSG ## name?

2)是否有解决方法?

2) Is there a workaround?

是否存在像define(x)这样的预处理指令,例如expand(x)?

Is there a preprocessor directive like defined(x), such as expand(x)?

推荐答案

#define MSG Hello
#define cat(x, y) x ## y
#define cat2(x, y) cat(x, y)
#define HELLO(name) cat2(MSG,name)

实时演示@ ideone

这篇关于C预处理器标记化不扩展宏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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