断言检查的便捷策略

发布于 2024-08-04 18:35:48 字数 119 浏览 6 评论 0原文

有些断言的成本很高,有些断言最好在生产代码中关闭。 至少目前还不清楚是否应始终启用断言。

在我的应用程序中,我希望能够基于每个文件或每个类打开/关闭部分断言。

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

站稳脚跟 2024-08-11 18:35:48

为了在模块范围内停用断言,我会使用:

#if defined(assert)
#  undef assert
#  define assert(x) ((void)0)
#endif

...当然,如果您可以使用自定义宏,则可以简化这一点。

#if defined(_NO_ASSERTS)
#  define myAssert(x) ((void)0)
#else
#  define myAssert(x) assert(x)
#endif

对于类范围的停用,我将使用静态 const 类成员或类范围枚举与自定义宏结合使用:

#define myAssert(x) do { if(_CLASS_ASSERT) { assert(x); } } while(0)

class AssertOff
{
  enum { _CLASS_ASSERT = 0 }
}

使用枚举和静态 const 布尔值,所有现代编译器都应该优化 if(_CLASS_ASSERT) {}< /代码>。

For deactivating asserts module-wide i'd use:

#if defined(assert)
#  undef assert
#  define assert(x) ((void)0)
#endif

... of course this can be simplified if you are okay with using a custom macro.

#if defined(_NO_ASSERTS)
#  define myAssert(x) ((void)0)
#else
#  define myAssert(x) assert(x)
#endif

For class-wide deactivation i'd use a static const class member or a class-wide enum in combination with a custom macro:

#define myAssert(x) do { if(_CLASS_ASSERT) { assert(x); } } while(0)

class AssertOff
{
  enum { _CLASS_ASSERT = 0 }
}

With enums and static const bools all modern compilers should optimize away the if(_CLASS_ASSERT) {}.

燃情 2024-08-11 18:35:48

使用断言进行编码被认为是良好的编码风格。

至于运行时打开/关闭您可以使用布尔变量来做到这一点。例如,在您的代码中,您可以执行以下操作:

定义一个变量,该变量将用于指示是否在全局命名空间中打开或关闭断言(例如在同一文件中的 main() 函数之外)。

bool  turnOnAssertions;

在您想要打开/关闭断言的其他文件中定义一个变量,如下所示:

extern bool turnOnAssertions; 

因此,通过使用 UI 操作 TurnOnAssertions 变量并写入,

if(turnOnAssertions)
assert(…);

您可以打开/关闭某些断言!

至于编译时间,您应该执行以下操作:

对于您的编译器,您应该给出一个类似 –DASSERTIONSON (-Dflag_name [您可以设置任何您想要的标志名称]) 的标志,

#ifdef ASSERTIONSON 
bool turnOnAssertions = true;
#else
bool turnOnAssertions = false;
#endif

然后只使用该变量。

祝你好运!

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).

bool  turnOnAssertions;

Define a variable as written below in other files where you want to turn on/off your assertions:

extern bool turnOnAssertions; 

So by manipulating the turnOnAssertions variable with the UI and writing

if(turnOnAssertions)
assert(…);

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])

#ifdef ASSERTIONSON 
bool turnOnAssertions = true;
#else
bool turnOnAssertions = false;
#endif

And just use the variable.

Good luck!

檐上三寸雪 2024-08-11 18:35:48

要禁用 C++ 文件的断言,您可以执行以下操作之一:

  • 在源文件顶部附近定义 NDEBUG 常量。
  • -DNDEBUG 添加到源文件的编译选项中。

大多数 IDE 和/或构建基础设施允许您为每个文件指定构建选项,因此这是一个简单的解决方案。

当多个类混合到同一个源文件中,或者头文件中有很多内联函数时,基于每个类关闭断言会更加困难。您当然可以在相关位置#define NDEBUG#undef NDEBUG

由于某些 IDE 期望能够为非调试版本设置 NDEBUG ,因此您可以通过选择自己的宏名称(例如 DISABLE_ASSERT )来使其更具可扩展性。然后在公共头文件中包含如下代码(未预编译):

 #ifdef DISABLE_ASSERT
 #define NDEBUG
 #endif

To disable assertions for a C++ file, you could do one of the following:

  • Define the NDEBUG constant near the top of the source file.
  • Add the -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 as DISABLE_ASSERT. Then include code like like the following in a common header file (that isn't precompiled):

 #ifdef DISABLE_ASSERT
 #define NDEBUG
 #endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文