#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" );
来同时打印出信息。