[英]Is it possible to have a zero-cost assert() such that code should not have to be modified between debug and release builds?
I've noticed that some code often looks like this: 我注意到一些代码通常看起来像这样:
#ifdef DEBUG
assert(i == 1);
#endif //DEBUG
and that you may have several blocks of these sitting around in your raw code. 并且您的原始代码中可能有几个代码块。 Having to write out each block is tedious and messy.
必须写出每个块是乏味且混乱的。
Would it be plausible to have a function like this: 具有这样的功能是否合理:
auto debug_assert = [](auto expr) {
#ifdef DEBUG
assert(expr);
#endif //DEBUG
};
or something like this: 或类似这样的东西:
#ifdef DEBUG
auto debug_assert = [](bool expr) {
assert(expr);
};
#else //DEBUG
void debug_assert(bool expr) {}
#endif //DEBUG
to get a zero-cost assert when the DEBUG flag is not specified? 未指定DEBUG标志时得到零成本的断言? (ie it should have the same effect as if it was not put into the code without the lambda running, etc. and be optimized out by the g++/clang compilers).
(即,它应具有与未运行lambda的情况下未放入代码中的效果相同,并由g ++ / clang编译器进行优化)。
As mentioned by @KerrekSB, you can already disable asserts by defining NDEBUG
before including <cassert>
. 如@KerrekSB所述,您可以通过在包含
<cassert>
之前定义NDEBUG
来禁用断言。 The best way to ensure that it's defined before including the header file is to list it in as the argument to the compiler (with gcc it's -DNDEBUG
) 确保在包含头文件之前已对其进行定义的最佳方法是将其作为编译器的参数列出(对于gcc来说是
-DNDEBUG
)
Note: the assert
is removed by replacing it with a no-op expression, and there, the argument isn't evaluated at all (which is different from your suggested solution)! 注:
assert
通过与无操作表达式来替换它删除,并在那里所有的参数不计算(这是从你的建议的解决方案不同)! This is why it's of utmost importance to not call any functions that have side effects in assert
. 这就是为什么最重要的是不要在
assert
调用任何有副作用的函数。
For completeness: here is how assert
can be implemented: 为了完整性:这是
assert
实现方式:
#include <cstdio>
#include <cstdlib>
#ifndef NDEBUG
#define assert(EXPRESSION) ((EXPRESSION) ? (void)0 : (printf("assertion failed at line %d, file %s: %s\n", __LINE__, __FILE__, #EXPRESSION), exit(-1)))
#else
#define assert(EXPRESSION) (void)0
#endif
Introducing your own assert
-style macro is very commonly done. 引入自己的
assert
-style宏非常常见。 There are quite a lot of reasons you may want to do this: 您可能有很多原因要这样做:
REQUIRE
and how they use expression templates to decompose the expression into individual elements and stringify them) REQUIRE
以及它们如何使用表达式模板将表达式分解为单个元素并将其字符串化) exit()
ing the program, like throwing an exception, mailing the developer, logging to a file, breaking into the debugger exit()
程序外,您还想执行其他操作,例如引发异常,向开发人员发送邮件,登录到文件,进入调试器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.