jac*_*hab 8 macros c-preprocessor
假设我有以下宏:
#define xxx(x) printf("%s\n",x);
Run Code Online (Sandbox Code Playgroud)
现在在某些文件中我想使用此宏的"增强"版本而不更改其名称.新版本探索了原始版本的功能,并做了一些更多的工作.
#define xxx(x) do { xxx(x); yyy(x); } while(0)
Run Code Online (Sandbox Code Playgroud)
这当然给了我重新警告但是为什么我在这个范围内没有声明'xxx'?我该如何正确定义?
编辑:根据这个http://gcc.gnu.org/onlinedocs/gcc-3.3.6/cpp/Self_002dReferential-Macros.html它应该是可能的
小智 4
自引用宏根本不起作用:
http://gcc.gnu.org/onlinedocs/cpp/Self_002dReferential-Macros.html#Self_002dReferential-Macros
如果您正在使用 C++,您可以使用模板函数和命名空间获得相同的结果:
template <typename T> void xxx( x ) {
printf( "%s\n", x );
}
namespace my_namespace {
template <typename T> void xxx( T x ) {
::xxx(x);
::yyy(x);
}
}
Run Code Online (Sandbox Code Playgroud)