C中的宏扩展

前端之家收集整理的这篇文章主要介绍了C中的宏扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个宏

#define f(x) x x
     f (1
     #undef f
     #define f 2
     f)

根据this link扩展到此1 2 1 2.

它实际上是这样做的,我已经使用Xcode产品验证了>执行操作>预处理但扩展此宏时预处理器遵循哪些步骤?

解决方法

初步情况:

f (1
#undef f
#define f 2
f)

如果我们引用您提供的链接,则会通过两个步骤对宏进行预处理:
第1步:论证预扩展

If,within a macro invocation,that macro is redefined,then the new
definition takes effect in time for argument pre-expansion

函数式宏作为参数的替换为2:

f(1 f) -> f (1 2)

第2步:参数替换

but the original definition is still used for argument replacement

使用其原始定义解析类似函数的宏f:

f(1 2) -> 1 2 1 2

整件事实际上等同于以下内容

#define f(x) x x  
#define g 2
f(1 g)

猜你在找的Xcode相关文章