好的,这是另一种方法,包括打印参数名称和类型。它不是很优雅,有两个原因:
- 对于您发送的每个参数,它都需要一个新宏,并且...
- 真是吓人的宏!我从未见过它们被优雅地使用(这也不例外)。
所以这里是:
#include <iostream>
#define DETECTOR_0ARG_FUNC(RETTYPE, NAME) \
RETTYPE NAME() \
{ \
std::cout << "Function Name: " #NAME << std::endl; \
std::cout << "Returns: " #RETTYPE << std::endl; \
std::cout << "No Parameters" << std::endl;
#define DETECTOR_1ARG_FUNC(RETTYPE, NAME, PARAM1TYPE, PARAM1NAME) \
RETTYPE NAME(PARAM1TYPE PARAM1NAME) \
{ \
std::cout << "Function Name: " #NAME << std::endl; \
std::cout << "Returns: " #RETTYPE << std::endl; \
std::cout << "Pameter 1 (" #PARAM1TYPE " " #PARAM1NAME "): " << PARAM1NAME << std::endl;
#define DETECTOR_RETURN(RETTYPE, VALUE) \
RETTYPE __retval = (VALUE); \
std::cout << "Returning: " << __retval << std::endl << std::endl; \
return __retval;
#define DETECTOR_END_FUNC \
}
DETECTOR_0ARG_FUNC(int, GetFiveFactorial)
int result = 1;
for(int i=5; i>0; i--)
{
result = result * i;
}
DETECTOR_RETURN(int, result)
DETECTOR_END_FUNC
DETECTOR_1ARG_FUNC(int, GetFactorial, int, initVal)
int result = 1;
for(int i=initVal; i > 0; i--)
{
result = result * i;
}
DETECTOR_RETURN(int, result);
DETECTOR_END_FUNC
int main(int argc, char **argv)
{
GetFiveFactorial();
GetFactorial(7);
return 0;
}
输出:
Function Name: GetFiveFactorial
Returns: int
No Parameters
Returning: 120
Function Name: GetFactorial
Returns: int
Pameter 1 (int initVal): 7
Returning: 5040
我有点不建议你这样做。但从理论上讲,这种方式是可能的。