如何定义具有依赖于符号调试的参数的函数
How can I define a function with a parameter dependent on a symbol debug?
我用 -D 编译器选项定义了符号调试:-DDEBUG_VALUE。我想要一个函数,其中参数的存在取决于符号调试标志的定义或更少。
即如果定义了DEBUG_VALUE,我有
my_function(int parameter1, int my_parameter_dependent)
否则
my_function(int parameter1)
以这种方式
my_function(int parameter1 #ifdef DEBUG_VALUE , int my_parameter_dependent #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 );
它们都有其优点,具体取决于灵活性和更改的功能数量。
不能在一行中嵌入预处理器宏。他们需要自己的专用线路。因此,您必须将此声明分解为单独的行:
#ifdef DEBUG_VALUE
void my_function(int parameter1, int my_parameter_dependant);
#else
void my_function(int parameter1);
#endif
或者,如果你想变得聪明和干燥,利用C++在语句和空格方面的巨大灵活性:
void my_function(int parameter1
#ifdef DEBUG_VALUE
, int my_parameter_dependant
#endif
);
相关文章:
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 为什么内存屏障依赖于变量?
- 反转依赖于 end() 的迭代器
- 编译依赖于 QTCore 库的 WASM
- 如何添加依赖于类本身的模板成员变量
- 初始化依赖于子类的继承类的常量类成员
- Makefile 创建两个库 - 一个依赖于另一个
- 对于BTreeMap和其他依赖于Ord的东西,是否有等效于C++比较器对象?
- 类成员函数参数列表是否可以依赖于模板参数?
- 依赖于类成员属性的类实例成员
- 如何定义依赖于参数包转换的函数的返回类型
- 依赖于模板的错误
- 依赖于特定类类型的C++模板方法
- FBString 的小字符串优化是否依赖于未定义的行为?
- CMake:如何在Visual Studio环境中将依赖于模式的编译标志传递给nvcc
- C++ 使函数调用依赖于模板参数
- 如何初始化依赖于先前条目的可变参数模板?
- cmake:构建依赖于非源文件.Qt帮助生成
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 如何定义具有依赖于符号调试的参数的函数