assert() 是否充当发布模式下的身份函数?

Does assert() act as the identity function in release mode?

This library uses assert() as if it's the identity function in release mode (when NDEBUG is defined). The problem is that some important code is wrapped with assert(), and my tests fired when executed in release mode, because these important parts were not called. An example of this can be found here,其中随机字节生成器不会生成任何内容,并会导致无限循环。

个人轶事:我不喜欢 assert() 并且由于这些歧义问题我个人不使用它。我听说许多项目因此而存在严重错误,最近的一次是 EOS,当时他们的单元测试没有检测到一些超出范围的数组,因为 NDEBUG 是在发布模式下定义的,并且没有触发。在这一点上,文档似乎并不清楚。 assert() 完全充当身份吗?[=​​15=]

这个库(libbtc)好像被广泛使用了,我不明白开发者为什么要这么做。这是一个可怕的错误吗,我应该分叉并删除所有这些断言吗?或者这是一些与 C++ 不兼容的 C 东西?有人可以在这里解释正确的操作步骤吗?

我用的是 clang 6.

来自 https://en.cppreference.com/w/cpp/error/assert :

If NDEBUG is defined as a macro name at the point in the source code where <cassert> is included, then assert does nothing.

不要在 assert 中放入任何具有您所依赖的副作用的东西。当您编译发布时它们不会发生,并且会改变您的程序的行为。

看起来库编写者误用了断言并将功能代码放在了不属于它的地方。 至于 assert() 的错误,它的发生是因为对其目的的误解或缺乏编程纪律。 assert() 用于验证不变量不会被破坏,而不是用于功能代码。 assert() 很容易误用或用错地方。