[英]C++ Replacing Functions with Macros
We have an existing function implementation in our C++ code: 我们的C ++代码中已有一个函数实现:
void Function(int param)
{
printf("In Function\n");
}
int main()
{
Function(10);
return 0;
}
I wish to change it to call another function (by help of a macro declaration) which would accept additional params like FILE and LINE (for debugging purpose) and then call the actual function: 我希望将其更改为调用另一个函数(借助于宏声明),该函数将接受其他参数,例如FILE和LINE (出于调试目的),然后调用实际函数:
#define Function(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__) \
{\
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);\
}
But the below code: 但是下面的代码:
#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;
}
Translates TO: 转换为:
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;
}
which gives compilation errors. 给出编译错误。
Please direct me how to achieve the objective? 请指导我如何实现目标?
Normally you'd do something like this: 通常,您会执行以下操作:
#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;
}
1st rule: use different name for your macro and the function that it replaces. 第一条规则:为宏及其替换的函数使用不同的名称。
2nd rule: if using gcc, wrap the code block in parenthesis ({ }) to make your functions callable from as if printf("%f\\n",sqrt(2.0));
第二条规则:如果使用gcc,则将代码块包装在括号({})中,以使您的函数可以像
printf("%f\\n",sqrt(2.0));
那样被调用printf("%f\\n",sqrt(2.0));
#define Function(a) ({ printf("Debug stuff\\n"); orig_function(a);})
You should first implement a second function and then in your macro just redirect your code to it, in your version you implement the function everywhere that use macro: 您应该首先实现第二个功能,然后在宏中将代码重定向到它,在您的版本中,您到处都使用宏来实现该功能:
void function( int param )
{
// do something interesting here
}
void function_debug( int param, char const* fileName, unsigned line, char const* fn)
{
// debug position here
function( param );
}
#ifndef NDEBUG
# define Function( param ) function_debug( param, __FILE__, __LINE__, __FUNCION__ )
#else
# define Function( param ) function( param )
#endif
can't you add the debug print into Function
? 不能将调试打印添加到
Function
吗?
void Function(int param)
{
#ifdef DEBUG
//debug stuff
#endif
printf("In Function\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.