两级宏&&字符串化宏

如果你想字符串化宏参数扩展的结果,你必须使用两个级别的宏。

1 #define xstr(s) str(s)
2 #define str(s) #s
3 #define foo 4
4 str (foo)
5      ==> "foo"
6 xstr (foo)
7      ==> xstr (4)
8      ==> str (4)
9      ==> "4"

当s被用在str中,s被字符串化,所以首先它不是宏扩展。

但s是xstr的一个普通参数,所以在xstr完全宏扩展之前s本身已经展开。

STR到达它的参数的时候,它已经是宏扩展。就是xstr处理自身参数foo的时候,foo已经展开。

解决方案出处

https://gcc.gnu.org/

posted @ 2014-09-29 21:59  青竹居士  阅读(189)  评论(0编辑  收藏  举报