[英]Turning off debugging, NDEBUG
C++ Primer says that C ++ Primer说
The behavior of assert depends on the status of a preprocessor variable named NDEBUG. assert的行为取决于名为NDEBUG的预处理程序变量的状态。 We can "turn off" debugging by providing a
#define
to defineNDEBUG
我们可以通过提供#define
定义NDEBUG
来“关闭”调试
It is my expectation that when define is provided, asserts won't be executed. 我期望当提供define时,断言将不会被执行。
#define NDEBUG TRUE
int main (int argc, char const *argv[])
{
assert(argc==0); // checked
return 0;
}
Why, in this example, is assert
statement checked, when NDEBUG
is defined? 在此示例中,为什么在定义NDEBUG
时检查assert
语句? (Correct me if i am wrong, but it does not matter to what it is defined, right?) (如果我错了,请更正我,但是定义的内容无关紧要,对吧?)
When executed from command line, using the -DNDEBUG
flag, all works as expected ( assert
is not executed) 使用-DNDEBUG
标志从命令行执行时,所有操作-DNDEBUG
预期进行(未执行assert
)
NDEBUG
only affects assert
if you define it before including <cassert>
(or <assert.h>
; note that you can include these headers multiple times changing the behaviour of assert
depending on NDEBUG
). NDEBUG
仅在包含<cassert>
(或<assert.h>
之前定义assert
时会影响assert
。请注意,可以多次包含这些标头,从而根据NDEBUG
改变assert
的行为。
You don't need to define it to any specific value, or any value at all: 您无需将其定义为任何特定值,或者根本不需要将其定义为:
// this is OK
#define NDEBUG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.