c++debuggingassertdebugbreak

What is the best way of implementing assertion checking in C++?


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).


Solution

  • #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