我用 -D 编译器选项定义了一个调试符号:-DDEBUG_VALUE 我想要一个函数,其中参数的存在取决于符号调试标志的定义或更少。
也就是说,如果我定义了 DEBUG_VALUE
my_function(int parameter1 ,int my_parameter_dependant)
否则
my_function(int parameter1)
这样
my_function(int parameter1 #ifdef DEBUG_VALUE , int my_parameter_dependant #endif)
我明白了
error: stray ‘#’ in program
error: expected ‘,’ or ‘...’ before ‘ifdef’
我该如何解决? 提前致谢!
(我在Unix系统上使用C++编译器)
最佳答案
您可以用不同的方式声明函数...
#if defined( DEBUG_VALUE )
void my_function( int parameter1, int my_parameter_dependent );
#else
void my_function( int parameter1 );
#endif
创建一个嵌入式宏
# if defined( DEBUG_VALUE )
#define DEPENDENT_PARAM( x ) x
# else
#define DEPENDENT_PARAM( x )
#endif
void my_function( int parameter1 DEPENDENT_PARAM(, int my_parameter_dependent) );
这意味着宏中的文本被预处理器处理并被隐藏
或者你可以声明调试数据
#if defined( DEBUG_VALUE )
#define EXTRA_DEBUG , int my_parameter_dependent
#else
#define EXTRA_DEBUG
#endif
void my_function( int parameter1 EXTRA_DEBUG );
它们各有千秋,就看灵 active 和改变多少功能了。
关于c++ - 如何使用依赖于符号调试的参数定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594849/