在C ++中实现断言检查的最好方法是什么? [英] What is the best way of implementing assertion checking in C++?
问题描述
我需要做的是在我的代码中使用有用的断言。MFC很容易,我只是使用ASSERT(东西)。
非MFC方式是什么?
编辑:停止断言assert.c而不是我的文件调用assert()?
编辑:< assert.h>
& < cassert>
接受答案 ,我希望我可以接受一个以上的答案(或有人会把他们所有)。所以答案被授予Ferruccio(第一个答案)。
#include< cassert>
assert(something);
并且对于编译时检查,Boost的静态断言非常有用:
#include< boost / static_assert.hpp>
BOOST_STATIC_ASSERT(sizeof(int)== 4); //如果int不是32位则编译失败
By that I mean, what do I need to do to have useful assertions in my code?
MFC is quite easy, i just use ASSERT(something).
What's the non-MFC way?
Edit: Is it possible to stop assert breaking in assert.c rather than than my file which called assert()?
Edit: What's the difference between <assert.h>
& <cassert>
?
Accepted Answer: Loads of great answers in this post, I wish I could accept more than one answer (or someone would combine them all). So answer gets awarded to Ferruccio (for first answer).
#include <cassert>
assert(something);
and for compile-time checking, Boost's static asserts are pretty useful:
#include <boost/static_assert.hpp>
BOOST_STATIC_ASSERT(sizeof(int) == 4); // compile fails if ints aren't 32-bit
这篇关于在C ++中实现断言检查的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!