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

标签 c++ c++11 compiler-errors compiler-flags compiler-options

我用 -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/

相关文章:

c++ - 在 C++ 中录制时从设备获取音量

C++ : "undefined reference to WeightCalc::getHeavier(int)"

c++ - lambda 到 curl 回调函数

java - 无法将构建器模式的方法调用与已编译的内部类链接起来

c++ - 编译器错误 `<<`

c++ - C++ 抽象类的问题(我可以在 Java 中完成,但不能在 C++ 中完成!)

c++ - 安全地将 printf 打印到字符串的最佳方法?

c++ - 通过类型的变量键入特征

c++ - 从类类型到类类型的隐式转换

c++ - 使用可隐式转换为模板类对象的参数调用函数