如何使用先前的定义重新定义宏 [英] How to redefine a macro using its previous definition
问题描述
假设我有以下宏:
#define xxx(x) printf("%s\n",x);
现在在某些文件中,我想使用此宏的增强型"版本而不更改其名称.新版本探索了原始版本的功能并做了更多工作.
Now in certain files I want to use an "enhanced" version of this macro without changing its name. The new version explores the functionality of the original version and does some more work.
#define xxx(x) do { xxx(x); yyy(x); } while(0)
这当然给了我重新定义警告,但是为什么未在此范围内声明我得到"xxx"的信息?我应该如何正确定义它?
This of course gives me redefition warning but why I get 'xxx' was not declared in this scope? How should I define it properly?
根据此 http://gcc.gnu.org/onlinedocs/gcc-3.3.6/cpp/Self_002dReferential-Macros.html 应该有可能
推荐答案
自引用宏根本不起作用:
Self-referential macros do not work at all:
http://gcc.gnu.org/onlinedocs /cpp/Self_002dReferential-Macros.html#Self_002dReferential-Macros
如果您使用的是C ++,则可以使用模板函数和名称空间获得相同的结果:
If you're working on C++ you can obtain the same results with template functions and namespaces:
template <typename T> void xxx( x ) {
printf( "%s\n", x );
}
namespace my_namespace {
template <typename T> void xxx( T x ) {
::xxx(x);
::yyy(x);
}
}
这篇关于如何使用先前的定义重新定义宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!