The C assert( ) macro (C语言的一种宏应用:断言)

The C assert( ) macro (C语言的一种宏应用:断言)

In the standard header file <cassert>,you'll find assert( ), which is a
在标准的头文件<cassert>中有一个asser(),它是宏的一个应用,用它可以
convenient debugging macro. When you use assert( ), you give it
很方便的进行排错。我们在写程序时,可以给这个方法指定一表达式,通过此表达式
an argument that is an expression you are "asserting to be true".
可以进行是否为真判断。
The preprocessor generates code that will test the assertion. If the
C语言的预处理系统会产生相应的代码进行断言测试。
assertion isn't true, the program will stop after issuing an error
如果断言为假的话,程序会在发出一条信息(这条信息告诉你有关此断言的信息)后停止
message telling you what the assertion was and that it failed.

Here's a trivial example:
下面的例子没有什么实用价值,但很能说明断言的应用:

//: C03:Assert.cpp
// Use of the assert() debugging macro
#include <cassert>  // Contains the macro
using namespace std;
 
int main() {
  int i = 100;
  assert(i != 100); // Fails
} ///:~
 
The macro originated in Standard C, so it's also available in the
断言是源自于标准C语言的,所以在头文件asser.h中也可用。
header file assert.h. 
When you are finished debugging, you can remove the code
在据断言报错的信息排错后,你就可以通过在"#include<cassert>"之前加一条#define NDEBUG语句来
generated by the macro by placing the line:
去掉宏已生成的的相关代码,
#define NDEBUG 

in the program before the inclusion of <cassert>, or by defining
通过在编译器的命令行中加一NDEBUG也可以达到此效果。
NDEBUG on the compiler command line. NDEBUG is a flag used
NDEBUG是在<cassert>中用到的一个标志,用它可以改变宏产生代码的方式。
in <cassert> to change the way code is generated by the macros.
Later in this book, you'll see some more sophisticated alternatives
在本书的后面章节中,你会看到一个替代断言的更为娴熟的方法。
to assert( ) .  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值