2013年6月17日 星期一

Adding message to assert


為了讓程式更好偵測問題發生的原因
再使用Assert時最好加上一些訊息

assert(length >= 0); // die if length is negative.


可以更為明瞭的做法
assert(length >= 0 && "Whoops, length can't possibly be negative! (didn't we just check 10 lines ago?) Tell kirenenko");


或者
assert(("Length can't possibly be negative! Tell jsmith", length >= 0));



其實還可以
assert((length >= 0) , "length was " << length);

 

沒有留言: