2.如何用好ASSERT宏让ASSERT更好用(定制自己的Assert)

来源:互联网 发布:流水号生成算法 编辑:程序博客网 时间:2024/06/05 01:23

我们可以自己定义一个宏,让宏接受2个参数,其中一个参数是判断的条件,另外一个是弹出的调试信息。

#include <assert.h>#include <string.h>#define Assert(a,b) assert(a&&b)int _tmain(int argc, _TCHAR* argv[]){Assert(0,"Nvidia_insides'Blog");}

当然,这些只是简单的使用assert,我们要更好的使用assert宏,我们就要定制自己的assert。

#include <assert.h>#include <string.h>#include <Windows.h>bool CustomAssertFunction(bool isfalse,char* description,char* filepath,int line)//Assert执行的函数{if(true == isfalse)return false;printf("需要调试的位置为 %s 中的 %d 行\n",filepath,line);if(IDOK== MessageBoxA(0,description,"Assert调试",MB_OKCANCEL))return true;else return false;}#if defined( _DEBUG )//如果在debug模式//定义assert宏,用if判断是否进行{ int 3 }调试#define Assert(exp,description) \if(CustomAssertFunction(int(exp),description,__FILE__, __LINE__)) \{_asm{int 3}}#else#define Assert(exp,description)#endifint _tmain(int argc, _TCHAR* argv[]){Assert(1,"Nvidia_insides'Blog");}



怎么样,很神奇吧~!





0 0