DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
#include<iostream>

#undef NDEBUG
#include<cassert>

using namespace std;

int main()
{
    assert(1==2	);
    cout<<"here"<<endl;
}

输出

Assertion failed: 1==2, file E:\codes\try\cppAssert\main.cpp, line 10

这就是先消除NDEBUG宏,从而可以让这个文件内部使用断言。
但是要注意:在cpp文件内使用,而不要在h文件内使用,因为宏是可以传递的。

这样,cmake 的release mode也是可以用assert的。

但是一定要注意!一定要把#undef NDEBUG放到#include的前面!否则就不起作用!

此外,可以妙用&&

assert(1==2 &&"the assert info"	);

来同时打印出信息。

 
posted on 2023-05-19 17:30  DoubleLi  阅读(283)  评论(0)    收藏  举报