assert.adoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ////
  2. Copyright 2002, 2007, 2014, 2017 Peter Dimov
  3. Copyright 2011 Beman Dawes
  4. Copyright 2015 Ion Gaztañaga
  5. Distributed under the Boost Software License, Version 1.0.
  6. See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt
  8. ////
  9. # Assertion Macros, <boost/assert.hpp>
  10. :toc:
  11. :toc-title:
  12. :idprefix:
  13. ## BOOST_ASSERT
  14. The header `<boost/assert.hpp>` defines the macro `BOOST_ASSERT`,
  15. which is similar to the standard `assert` macro defined in `<cassert>`.
  16. The macro is intended to be used in both Boost libraries and user
  17. code.
  18. * By default, `BOOST_ASSERT(expr)` expands to `assert(expr)`.
  19. * If the macro `BOOST_DISABLE_ASSERTS` is defined when `<boost/assert.hpp>`
  20. is included, `BOOST_ASSERT(expr)` expands to `((void)0)`, regardless of whether
  21. the macro `NDEBUG` is defined. This allows users to selectively disable `BOOST_ASSERT` without
  22. affecting the definition of the standard `assert`.
  23. * If the macro `BOOST_ENABLE_ASSERT_HANDLER` is defined when `<boost/assert.hpp>`
  24. is included, `BOOST_ASSERT(expr)` expands to
  25. +
  26. ```
  27. (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed(#expr,
  28. BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
  29. ```
  30. +
  31. That is, it evaluates `expr` and if it's false, calls
  32. `::boost::assertion_failed(#expr, <<current_function.adoc#boost_current_function,BOOST_CURRENT_FUNCTION>>, \\__FILE__, \\__LINE__)`.
  33. This is true regardless of whether `NDEBUG` is defined.
  34. +
  35. `boost::assertion_failed` is declared in `<boost/assert.hpp>` as
  36. +
  37. ```
  38. namespace boost
  39. {
  40. void assertion_failed(char const * expr, char const * function,
  41. char const * file, long line);
  42. }
  43. ```
  44. +
  45. but it is never defined. The user is expected to supply an appropriate definition.
  46. * If the macro `BOOST_ENABLE_ASSERT_DEBUG_HANDLER` is defined when `<boost/assert.hpp>`
  47. is included, `BOOST_ASSERT(expr)` expands to `((void)0)` when `NDEBUG` is
  48. defined. Otherwise the behavior is as if `BOOST_ENABLE_ASSERT_HANDLER` has been defined.
  49. As is the case with `<cassert>`, `<boost/assert.hpp>`
  50. can be included multiple times in a single translation unit. `BOOST_ASSERT`
  51. will be redefined each time as specified above.
  52. ## BOOST_ASSERT_MSG
  53. The macro `BOOST_ASSERT_MSG` is similar to `BOOST_ASSERT`, but it takes an additional argument,
  54. a character literal, supplying an error message.
  55. * By default, `BOOST_ASSERT_MSG(expr,msg)` expands to `assert\((expr)&&(msg))`.
  56. * If the macro `BOOST_DISABLE_ASSERTS` is defined when `<boost/assert.hpp>`
  57. is included, `BOOST_ASSERT_MSG(expr,msg)` expands to `((void)0)`, regardless of whether
  58. the macro `NDEBUG` is defined.
  59. * If the macro `BOOST_ENABLE_ASSERT_HANDLER` is defined when `<boost/assert.hpp>`
  60. is included, `BOOST_ASSERT_MSG(expr,msg)` expands to
  61. +
  62. ```
  63. (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr,
  64. msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
  65. ```
  66. +
  67. This is true regardless of whether `NDEBUG` is defined.
  68. +
  69. `boost::assertion_failed_msg` is declared in `<boost/assert.hpp>` as
  70. +
  71. ```
  72. namespace boost
  73. {
  74. void assertion_failed_msg(char const * expr, char const * msg,
  75. char const * function, char const * file, long line);
  76. }
  77. ```
  78. +
  79. but it is never defined. The user is expected to supply an appropriate definition.
  80. * If the macro `BOOST_ENABLE_ASSERT_DEBUG_HANDLER` is defined when `<boost/assert.hpp>`
  81. is included, `BOOST_ASSERT_MSG(expr)` expands to `((void)0)` when `NDEBUG` is
  82. defined. Otherwise the behavior is as if `BOOST_ENABLE_ASSERT_HANDLER` has been defined.
  83. As is the case with `<cassert>`, `<boost/assert.hpp>`
  84. can be included multiple times in a single translation unit. `BOOST_ASSERT_MSG`
  85. will be redefined each time as specified above.
  86. ## BOOST_VERIFY
  87. The macro `BOOST_VERIFY` has the same behavior as `BOOST_ASSERT`, except that
  88. the expression that is passed to `BOOST_VERIFY` is always
  89. evaluated. This is useful when the asserted expression has desirable side
  90. effects; it can also help suppress warnings about unused variables when the
  91. only use of the variable is inside an assertion.
  92. * If the macro `BOOST_DISABLE_ASSERTS` is defined when `<boost/assert.hpp>`
  93. is included, `BOOST_VERIFY(expr)` expands to `\((void)(expr))`.
  94. * If the macro `BOOST_ENABLE_ASSERT_HANDLER` is defined when `<boost/assert.hpp>`
  95. is included, `BOOST_VERIFY(expr)` expands to `BOOST_ASSERT(expr)`.
  96. * Otherwise, `BOOST_VERIFY(expr)` expands to `\((void)(expr))` when `NDEBUG` is
  97. defined, to `BOOST_ASSERT(expr)` when it's not.
  98. ## BOOST_VERIFY_MSG
  99. The macro `BOOST_VERIFY_MSG` is similar to `BOOST_VERIFY`, with an additional parameter, an error message.
  100. * If the macro `BOOST_DISABLE_ASSERTS` is defined when `<boost/assert.hpp>`
  101. is included, `BOOST_VERIFY_MSG(expr,msg)` expands to `\((void)(expr))`.
  102. * If the macro `BOOST_ENABLE_ASSERT_HANDLER` is defined when `<boost/assert.hpp>`
  103. is included, `BOOST_VERIFY_MSG(expr,msg)` expands to `BOOST_ASSERT_MSG(expr,msg)`.
  104. * Otherwise, `BOOST_VERIFY_MSG(expr,msg)` expands to `\((void)(expr))` when `NDEBUG` is
  105. defined, to `BOOST_ASSERT_MSG(expr,msg)` when it's not.
  106. ## BOOST_ASSERT_IS_VOID
  107. The macro `BOOST_ASSERT_IS_VOID` is defined when `BOOST_ASSERT` and `BOOST_ASSERT_MSG` are expanded to `((void)0)`.
  108. Its purpose is to avoid compiling and potentially running code that is only intended to prepare data to be used in the assertion.
  109. ```
  110. void MyContainer::erase(iterator i)
  111. {
  112. // Some sanity checks, data must be ordered
  113. #ifndef BOOST_ASSERT_IS_VOID
  114. if(i != c.begin()) {
  115. iterator prev = i;
  116. --prev;
  117. BOOST_ASSERT(*prev < *i);
  118. }
  119. else if(i != c.end()) {
  120. iterator next = i;
  121. ++next;
  122. BOOST_ASSERT(*i < *next);
  123. }
  124. #endif
  125. this->erase_impl(i);
  126. }
  127. ```
  128. * By default, `BOOST_ASSERT_IS_VOID` is defined if `NDEBUG` is defined.
  129. * If the macro `BOOST_DISABLE_ASSERTS` is defined, `BOOST_ASSERT_IS_VOID` is always defined.
  130. * If the macro `BOOST_ENABLE_ASSERT_HANDLER` is defined, `BOOST_ASSERT_IS_VOID` is never defined.
  131. * If the macro `BOOST_ENABLE_ASSERT_DEBUG_HANDLER` is defined, then `BOOST_ASSERT_IS_VOID` is defined when `NDEBUG` is defined.