我们的C++代码中有一个现有的函数实现:
void Function(int param)
{
printf("In Function\n");
}
int main()
{
Function(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望将其更改为调用另一个函数(通过宏声明的帮助),它将接受其他参数,如 FILE和LINE(用于调试目的),然后调用实际函数:
#define Function(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__) \
{\
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);\
}
Run Code Online (Sandbox Code Playgroud)
但是下面的代码:
#include <stdio.h>
#define Function(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__) \
{\
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);\
}
void Function(int param)
{
printf("In Function\n");
}
int main()
{
Function(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
翻译为:
void Function_debug(int param, "temp.cpp", __FUNCTION__, 9) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(int param);}
{
printf("In Function\n");
}
int main()
{
Function_debug(10, "temp.cpp", __FUNCTION__, 16) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(10);};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译错误.
请指导我如何实现目标?
通常你会做这样的事情:
#if DEBUG
#define FUNCTION(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__)
#else
#define FUNCTION(param) Function(param)
#endif
void Function(int param)
{
printf("In Function\n");
}
void Function_debug(int param, const char * file, const char * func, int line)
{
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);
}
int main()
{
FUNCTION(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)