如何使用依赖于符号调试的参数定义函数?

Umb*_*ert 2 c++ compiler-errors compiler-flags compiler-options c++11

我用-D编译器选项定义了一个符号debug:-DDEBUG_VALUE我是一个函数,其中一个参数的存在取决于符号调试标志的定义或更少.

即如果我定义了DEBUG_VALUE

my_function(int parameter1  ,int  my_parameter_dependant)
Run Code Online (Sandbox Code Playgroud)

除此以外

my_function(int parameter1)
Run Code Online (Sandbox Code Playgroud)

通过这种方式

my_function(int parameter1  #ifdef DEBUG_VALUE , int my_parameter_dependant #endif)
Run Code Online (Sandbox Code Playgroud)

我明白了

 error: stray ‘#’ in program
 error: expected ‘,’ or ‘...’ before ‘ifdef’
Run Code Online (Sandbox Code Playgroud)

我怎么解决?提前致谢!

(我在Unix系统上使用C++编译器)

mks*_*eve 5

你可以用不同的方式声明这个函数......

 #if defined( DEBUG_VALUE )
     void my_function( int parameter1, int my_parameter_dependent );
 #else
     void my_function( int parameter1 );
 #endif
Run Code Online (Sandbox Code Playgroud)

创建一个嵌入式宏

 # 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) );
Run Code Online (Sandbox Code Playgroud)

这意味着宏中的文本被预处理器碾碎,并被隐藏

或者您可以声明调试数据

  #if defined( DEBUG_VALUE )
      #define EXTRA_DEBUG  , int my_parameter_dependent  
  #else
      #define EXTRA_DEBUG
  #endif
  void my_function( int parameter1 EXTRA_DEBUG );
Run Code Online (Sandbox Code Playgroud)

它们都有其优点,取决于灵活性和改变了多少功能.