在C ++中实现断言检查的最好方法是什么? [英] What is the best way of implementing assertion checking in C++?

查看:246
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆