断言检查的便捷策略
有些断言的成本很高,有些断言最好在生产代码中关闭。 至少目前还不清楚是否应始终启用断言。
在我的应用程序中,我希望能够基于每个文件或每个类打开/关闭部分断言。
如何在 C++ 中做到这一点?
Some asserts are costly, some are better turned off at production code.
At least it is not clear that assertions should be always enabled.
In my application I would like to be able to turn on/off part of assertions on per-file or per-class basis.
How to do it in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了在模块范围内停用断言,我会使用:
...当然,如果您可以使用自定义宏,则可以简化这一点。
对于类范围的停用,我将使用静态 const 类成员或类范围枚举与自定义宏结合使用:
使用枚举和静态 const 布尔值,所有现代编译器都应该优化
if(_CLASS_ASSERT) {}< /代码>。
For deactivating asserts module-wide i'd use:
... of course this can be simplified if you are okay with using a custom macro.
For class-wide deactivation i'd use a static const class member or a class-wide enum in combination with a custom macro:
With enums and static const bools all modern compilers should optimize away the
if(_CLASS_ASSERT) {}
.使用断言进行编码被认为是良好的编码风格。
至于运行时打开/关闭您可以使用布尔变量来做到这一点。例如,在您的代码中,您可以执行以下操作:
定义一个变量,该变量将用于指示是否在全局命名空间中打开或关闭断言(例如在同一文件中的 main() 函数之外)。
在您想要打开/关闭断言的其他文件中定义一个变量,如下所示:
因此,通过使用 UI 操作 TurnOnAssertions 变量并写入,
您可以打开/关闭某些断言!
至于编译时间,您应该执行以下操作:
对于您的编译器,您应该给出一个类似 –DASSERTIONSON (-Dflag_name [您可以设置任何您想要的标志名称]) 的标志,
然后只使用该变量。
祝你好运!
To code with assertions considers good style of coding.
As for runtime turning on/off You may do that with Boolean variable. For example in your code you can do the following:
Define a variable which will be used to indicate if assertions are turned on or off in a global namespace (for example out of your main() function in the same file).
Define a variable as written below in other files where you want to turn on/off your assertions:
So by manipulating the turnOnAssertions variable with the UI and writing
you can turn on/off some of you assertions!
As for compile time you should do the following:
For you compiler you should give a flag like –DASSERTIONSON (-Dflag_name [flag name you can set anything you want])
And just use the variable.
Good luck!
要禁用 C++ 文件的断言,您可以执行以下操作之一:
-DNDEBUG
添加到源文件的编译选项中。大多数 IDE 和/或构建基础设施允许您为每个文件指定构建选项,因此这是一个简单的解决方案。
当多个类混合到同一个源文件中,或者头文件中有很多内联函数时,基于每个类关闭断言会更加困难。您当然可以在相关位置
#define NDEBUG
和#undef NDEBUG
。由于某些 IDE 期望能够为非调试版本设置 NDEBUG ,因此您可以通过选择自己的宏名称(例如 DISABLE_ASSERT )来使其更具可扩展性。然后在公共头文件中包含如下代码(未预编译):
To disable assertions for a C++ file, you could do one of the following:
NDEBUG
constant near the top of the source file.-DNDEBUG
to the compilation options for the source file.Most IDEs and/or build infrastructure allow you to specify build options per file, so this is an easy solution.
Turning off assertions on a per class basis is more difficult when multiple classes are mixed into the same source file, or you have a lot of inline functions in your header files. You can of course
#define NDEBUG
and#undef NDEBUG
in the relevant places.Since some IDEs expect to be able to set
NDEBUG
for non-debug builds, you could make it more extensible, by choosing your own macro name, such asDISABLE_ASSERT
. Then include code like like the following in a common header file (that isn't precompiled):