在阅读了一些关于异常误用的线程(基本上是说,如果函数的前提条件不正确,你不想解除堆栈-可能表明你所有的内存都已损坏或其他同样危险的事情)之后,我考虑更频繁地使用assert()。以前我只使用AsjtType()作为调试工具,我认为这就是很多C++程序员使用它的方式。我担心我的部分错误处理会被将来某个时候引入运行时构建的NDEBUG#define关闭。有没有办法解决这个问题,其他人是否对此有问题(即我是否应该担心)?
谢谢
拍打
编辑:
我读到的线程的要点是,如果应用程序真的出现错误,那么展开堆栈可能会损坏系统,例如,如果析构函数向文件写入了某些内容,并且文件句柄已损坏。我并不是建议使用assert进行正常的错误处理。我目前的用例很弱,但看看你怎么想:
//check later code won't crash the system
if( buf.length() % 2 )
return false;
// do other stuff that shouldn't affect bufs length
//copy 2 bytes into buf at a time, if length is odd then don't know
//what will happen so use assert to make sure it can't damage anything
assert( !(buf.length() % 2) );
for( i = 0; i != buf.length(); i += 2 )
memcpy( buf + i, data, 2 );
编辑2:讨论内容如下:
http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/80083ac31a1188da