Files
CPlusPlusThings/basic_content/assert
light-city dfe1c933b3 docs: address community issue feedback (#5 #338 #142 #246 #136 #303)
- const README: rewrite the space-saving claim per issue #5 follow-ups
  (constant-expression const vs #define immediates; which const qualify
  as constant expressions)
- const README: fix the class-section diagnosis per issue #338 (overload
  resolution picks the const overload; the restriction only surfaces
  once that overload is removed)
- sizeof README: virtual-inheritance result is compiler-dependent
  (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246
- static README: new section on internal linkage / access restriction
  with anonymous-namespace comparison #142
- 10-day practice: full problem list table + fixed day links #136
- new design_pattern/singleton/singleton_default.hpp: reusable
  thread-safe CRTP singleton template, compile+concurrency tested #303
2026-10-13 19:23:54 +08:00
..

本文档由 C++那些事网站 同步生成,网站版本含多语言与更新排版。

C++ assert 断言

关于作者:

个人公众号:

1.第一个断言案例

断言,是宏,而非函数。

assert 宏的原型定义在 <assert.h>(C)、(C++)中。其作用是如果它的条件返回错误,则终止程序执行。

可以通过定义 NDEBUG 来关闭 assert,但是需要在源代码的开头,include <assert.h> 之前。

void assert(int expression);

代码样例:assert.c

#include <stdio.h> 
#include <assert.h> 

int main() 
{ 
    int x = 7; 

    /*  Some big code in between and let's say x  
    is accidentally changed to 9  */
    x = 9; 

    // Programmer assumes x to be 7 in rest of the code 
    assert(x==7); 

    /* Rest of the code */

    return 0; 
} 

输出:

assert: assert.c:13: main: Assertion 'x==7' failed.

可以看到输出会把源码文件,行号错误位置,提示出来!

2.断言与正常错误处理

  • 断言主要用于检查逻辑上不可能的情况。

例如,它们可用于检查代码在开始运行之前所期望的状态,或者在运行完成后检查状态。与正常的错误处理不同,断言通常在运行时被禁用。

  • 忽略断言,在代码开头加上:
#define NDEBUG          // 加上这行,则 assert 不可用

样例代码:ignore_assert.c