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

后端 未结 11 1214
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 07:31

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?

相关标签:
11条回答
  • 2020-12-24 08:21
    #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
    
    0 讨论(0)
  • 2020-12-24 08:21

    To answer the question in your second "edit":

    < assert.h> is the C header

    < cassert> is the C++ Standard Library header ... it typically includes < assert.h>

    0 讨论(0)
  • 2020-12-24 08:26

    To answer the asker's third question: the first reason we use "cassert" instead of "assert.h" is because, in the case of C++, there's an allowance made for the fact that the C++ compiler may not store the function descriptions in code files, but in a dll or in the compiler itself. The second is that there may be minor changes made to functions in order to facilitate the differences between C and C++, either present or in the future. Because assert.h is a C library, the preference is to use "cassert" while in C++.

    0 讨论(0)
  • 2020-12-24 08:30

    use intellisense to open it in visual studio (right click)

    // cassert standard header
    #include <yvals.h>
    #include <assert.h>
    

    yvals.h is windows stuff. so, as far as assert() itself is concerned, the two ways to include it are identical. it's good practice to use the <cxxx> because often it isn't that simple (namespace wrapping and maybe other magic)

    This breaks at caller site for me...

    here's an article explaining why you don't want to write this macro yourself.

    0 讨论(0)
  • 2020-12-24 08:32

    It depends on whether or not you are looking for something that works outside of Visual C++. It also depends on what type of assertion you are looking for.

    There are a few types of assertions:

    1. Preprocessor
      These assertions are done using the preprocessor directive #error
      Preprocessor assertions are only evaluated during the preprocessing phase, and therefore are not useful for things such as templates.

    2. Execute Time
      These assertions are done using the assert() function defined in <cassert>
      Execute time assertions are only evaluated at run-time. And as BoltBait pointed out, are not compiled in if the NDEBUG macro has been defined.

    3. Static
      These assertions are done, as you said, by using the ASSERT() macro, but only if you are using MFC. I do not know of another way to do static assertions that is part of the C/C++ standard, however, the Boost library offers another solution: static_assert.
      The static_assert function from the Boost library is something that is going to be added in the C++0x standard.

    As an additional warning, the assert() function that Ferruccio suggested does not have the same behavior as the MFC ASSERT() macro. The former is an execute time assertion, while the later is a static assertion.

    I hope this helps!

    0 讨论(0)
提交回复
热议问题