消除与函数同名宏的扩展

#define F(x) (1)

int F(int x)
{
return 0 ;
}

int main()
{
// Micro is done at preprocess, which is ahead of compile
// So, if we write 'F(0)' must be expanded to micro
// How to call the function 'F' instead of micro 'F'?
// Here is a trick using '()'
// to call the same name function instead of micro
(F)(0) ; // Function
F(0) ; // Micro
return 0;
}

  

   这个技巧在C中常用于处理自定义函数的名字与标准库或商业库中的宏产生的冲突,或者希望直接选择使用标准库或商业库中的同名函数和宏。例如,C库的getchar既是宏也有函数实例:

int i = (getchar)();   /* 使用函数实例 */
i
= getchar(); /* 使用宏 */

  

posted @ 2011-08-07 21:55  walfud  阅读(244)  评论(0编辑  收藏  举报