宏扩展和参数扫描

referrence:

http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html#Argument-Prescan

Procedure

macro_expand (MACRO(ARGV))
/* 带参宏扩展过程,宏为MACRO, 参数为ARGV */
{
	/* 如果宏本身的定义为含有#, ##的宏,那么直接把参数ARGV当成字符串,代入宏,得到结果,退出 */	
	if ((MACRO contain "#") || (MACRO contain "##"))
	{
		stringified ARGV || ARGV pasted with other tokens
	}
	/* 如果宏不含#,##, 并且宏的参数ARGV本身也是个宏,那么先将宏参数扩展,宏本身不做任何改变,参数先不要代入 */
	else if ( ARGV is still an macro, which ARGV == NEW_MACRO (NEW_ARGV))
	{
		/* 
		that is to say macro_expand (ARGV) 
		ARGV == NEW_MACRO (NEW_ARGV)
		递归这个过程
		*/
		MACRO ( macro_expand (NEW_MACRO(NEW_ARGV)) )
	}
	/* 宏的参数为普通参数,不是一个宏,代入之,得到结果,从内往外代入,逐层得到结果。*/
	else /* ARGV is a plain argument */ 
	{
		expand the MACRO with argument ARGV
	}
}




three examples


First example:

	#define AFTERX(x) X_ ## x
	#define XAFTERX(x) AFTERX(x)
	#define TABLESIZE 1024
	#define BUFSIZE TABLESIZE
then AFTERX(BUFSIZE) expands to X_BUFSIZE, 
and XAFTERX(BUFSIZE) expands to X_1024. 
(Not to X_TABLESIZE. Prescan always does a complete expansion.)



Second example:



If you want to stringify the result of expansion of a macro argument, you have to use two levels of macros.

	#define xstr(s) str(s)
	#define str(s) #s
	#define foo 4
	str (foo)
		==> "foo"
	xstr (foo)
		==> xstr (4)
		==> str (4)
		==> "4"


s is stringified when it is used in str, so it is not macro-expanded first. 
But s is an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is expanded (see Argument Prescan). 
Therefore, by the time str gets to its argument, it has already been macro-expanded.


Third example:

#define a(x) b(x) + 1
#define b(x) c(x)
#define c(x)  #x
#define TABLE SIZE + 2
#define SIZE  5
 

/* right */
a(TABLE)
	==> a(SIZE + 2)
	==> a(5 + 2)
	==> b(5 + 2) + 1
	==> c(5 + 2) + 1
	==> "5 + 2" + 1
	
/* wrong */
a(TABLE) 
	==> b(TABLE) + 1
	==> c(TABLE) + 1
	==> "TABLE" + 1



本例为本人原创,已经通过代码测试。
主要表明,是先进行参数扩展,然后才进行参数替换,而不是先宏扩展,再参数替换。
测试代码参见我空间的代码。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值