extras.qbk 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. [/ Copyright (C) 2008-2018 Lorenzo Caminiti]
  2. [/ Distributed under the Boost Software License, Version 1.0 (see accompanying]
  3. [/ file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).]
  4. [/ See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html]
  5. [section Extras]
  6. This section can be consulted selectively for specific topics of interest.
  7. [section Old Value Requirements (Templates)]
  8. Old values require to copy the expression passed to [macroref BOOST_CONTRACT_OLDOF] thus the type of that expression needs to be copyable.
  9. More precisely, dereferencing an old value pointer of type [classref boost::contract::old_ptr]`<T>` requires [classref boost::contract::is_old_value_copyable]`<T>::value` to be `true`, otherwise this library will generate a compile-time error.
  10. In some cases it might be acceptable, or even desirable, to cause a compile-time error when a program uses old value types that are not copyable (because it is not possible to fully check the correctness of the program as stated by the contract assertions that use these old values).
  11. In these cases, programmers can declare old values using [classref boost::contract::old_ptr] as seen so far.
  12. However, in some other cases it might be desirable to simply skip assertions that use old values when the respective old value types are not copyable, without causing compile-time errors.
  13. Programmers can do this using [classref boost::contract::old_ptr_if_copyable] instead of [classref boost::contract::old_ptr] and checking if the old value pointer is not null before dereferencing it in postconditions.
  14. For example, consider the following function template that could in general be instantiated for types `T` that are not copy constructible (that is for which [classref boost::contract::is_old_value_copyable]`<T>::value` is `false`, see [@../../example/features/old_if_copyable.cpp =old_if_copyable.cpp=]):
  15. [footnote
  16. *Rationale:*
  17. __N1962__ and other proposals to add contracts to C++ do not provide a mechanism to selectively disable copies only for old value types that are not copy constructible.
  18. However, this library provides such a mechanism to allow to program contracts for template code without necessarily adding extra copy constructible type requirements that would not be present if it were not for copying old values (so compiling the code with and without contracts will not necessarily alter the type requirements of the program).
  19. Something similar could be achieved combing C++17 `if constexpr` with __N1962__ or __P0380__ so that old value expressions within template code can be guarded by `if constexpr` statements checking if the old value types are copyable or not.
  20. For example, assuming old values are added to __P0380__ (using some kind of `oldof(...)` syntax) and that C++17 `if constexpr` can be used within __P0380__ contracts:
  21. ``
  22. template<typename T>
  23. void offset(T& x, int count)
  24. [[ensures: if constexpr(std::is_copy_constructible<T>::value) x == oldof(x) + count]]
  25. ...
  26. ``
  27. ]
  28. [import ../example/features/old_if_copyable.cpp]
  29. [old_if_copyable_offset]
  30. The old value pointer `old_x` is programmed using [classref boost::contract::old_ptr_if_copyable].
  31. When `T` is copyable, [classref boost::contract::old_ptr_if_copyable]`<T>` behaves like [classref boost::contract::old_ptr]`<T>`.
  32. When `T` is not copyable instead, [classref boost::contract::old_ptr_if_copyable]`<T>` will simply not copy `x` at run-time and leave `old_x` initialized to a null pointer.
  33. Therefore, [classref boost::contract::old_ptr_if_copyable] objects like `old_x` must be checked to be not null as in `if(old_x) ...` before they are dereferenced in postconditions and exception guarantees (to avoid run-time errors of dereferencing null pointers).
  34. If the above example used [classref boost::contract::old_ptr] instead then this library would have generated a compile-time error when `offset` is instantiated with types `T` that are not copy constructible (but only if `old_x` is actually dereferenced somewhere, for example in the contract assertions using `*old_x ...` or `old_x->...`).
  35. [footnote
  36. Technically, on C++17 it is possible to use [classref boost::contract::old_ptr] together with `if constexpr` instead of using [classref boost::contract::old_ptr_if_copyable], for example:
  37. ``
  38. template<typename T>
  39. void offset(T& x, int count) {
  40. boost::contract::old_ptr<T> old_x;
  41. if constexpr(boost::contract::is_old_value_copyable<T>::value) old_x = BOOST_CONTRACT_OLDOF(x);
  42. boost::contract::check c = boost::contract::function()
  43. .postcondition([&] {
  44. if constexpr(boost::contract::is_old_value_copyable<T>::value) BOOST_CONTRACT_ASSERT(x == *old_x + count);
  45. })
  46. ;
  47. x += count;
  48. }
  49. ``
  50. However, the authors find this code less readable and more verbose than its equivalent that uses [classref boost::contract::old_ptr_if_copyable].
  51. Guarding old value copies and related assertions with `if constexpr` is useful instead when the guard condition checks type requirements more complex than just [classref boost::contract::is_old_value_copyable] (as shown later in this documentation).
  52. ]
  53. When C++11 `auto` declarations are used with [macroref BOOST_CONTRACT_OLDOF], this library always defaults to using the [classref boost::contract::old_ptr] type (because its type requirements are more stringent than [classref boost::contract::old_ptr_if_copyable]).
  54. For example, the following statements are equivalent:
  55. auto old_x = BOOST_CONTRACT_OLDOF(x); // C++11 auto declarations always use `old_ptr` (never `old_ptr_if_copyable`).
  56. boost::contract::old_ptr<decltype(x)> old_x = BOOST_CONTRACT_OLDOF(x);
  57. If programmers want to relax the copyable type requirement, they must do so explicitly by using the [classref boost::contract::old_ptr_if_copyable] type instead of using `auto` declarations.
  58. [heading Old Value Type Traits]
  59. This library uses [classref boost::contract::is_old_value_copyable] to determine if an old value type is copyable or not, and then [classref boost::contract::old_value_copy] to actually copy the old value.
  60. By default, [classref boost::contract::is_old_value_copyable]`<T>` is equivalent to `boost::is_copy_constructible<T>` and [classref boost::contract::old_value_copy]`<T>` is implemented using `T`'s copy constructor.
  61. However, these type traits can be specialized by programmers for example to avoid making old value copies of types even when they have a copy constructor (maybe because these copy constructors are too expensive), or to make old value copies for types that do not have a copy constructor, or for any other specific need programmers might have for the types in question.
  62. For example, the following specialization of [classref boost::contract::is_old_value_copyable] intentionally avoids making old value copies for all expressions of type `w` even if that type has a copy constructor (see [@../../example/features/old_if_copyable.cpp =old_if_copyable.cpp=]):
  63. [old_if_copyable_w_decl]
  64. [old_if_copyable_w_spec]
  65. On the flip side, the following specializations of [classref boost::contract::is_old_value_copyable] and [classref boost::contract::old_value_copy] make old value copies of expressions of type `p` even if that type does not actually have a copy constructor (see [@../../example/features/old_if_copyable.cpp =old_if_copyable.cpp=]):
  66. [old_if_copyable_p_decl]
  67. [old_if_copyable_p_spec]
  68. In general, `boost::is_copy_constructible` and therefore [classref boost::contract::is_old_value_copyable] require C++11 `decltype` and SFINAE to automatically detect if a given type is copyable or not.
  69. On non-C++11 compilers, it is possible to inherit the old value type from `boost::noncopyable`, or use `BOOST_MOVABLE_BUT_NOT_COPYABLE`, or specialize `boost::is_copy_constructible` (see [@http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/reference/is_copy_constructible.html `boost::is_copy_constructible`] documentation for more information), or just specialize [classref boost::contract::is_old_value_copyable].
  70. For example, for a non-copyable type `n` the following code will work also on non-C++11 compilers (see [@../../example/features/old_if_copyable.cpp =old_if_copyable.cpp=]):
  71. [old_if_copyable_n_decl]
  72. [old_if_copyable_n_spec]
  73. [endsect]
  74. [section Assertion Requirements (Templates)]
  75. In general, assertions can introduce a new set of requirements on the types used by the program.
  76. Some of these type requirements might be necessary only to check the assertions and they would not be required by the program otherwise.
  77. In some cases it might be acceptable, or even desirable, to cause a compile-time error when a program uses types that do not provide all the operations needed to check contract assertions (because it is not possible to fully check the correctness of the program as specified by its contracts).
  78. In these cases, programmers can specify contract assertions as we have seen so far, compilation will fail if user types do not provide all operations necessary to check the contracts.
  79. However, in some other cases it might be desirable to not augment the type requirements of a program just because of contract assertions and to simply skip assertions when user types do not provide all the operations necessary to check them, without causing compile-time errors.
  80. Programmers can do this using `if constexpr` on C++17 compilers, and [funcref boost::contract::condition_if] (or [funcref boost::contract::condition_if_c]) on non-C++17 compilers.
  81. For example, let's consider the following `vector<T>` class template equivalent to `std::vector<T>`.
  82. C++ `std::vector<T>` does not require that its value type parameter `T` has an equality operator `==` (it only requires `T` to be copy constructible, see `std::vector` documentation).
  83. However, the contracts for the `vector<T>::push_back(value)` public function include a postcondition `back() == value` that introduces the new requirement that `T` must also have an equality operator `==`.
  84. Programmers can specify this postcondition as usual with `BOOST_CONTRACT_ASSERT(back() == value)` an let the program fail to compile when users instantiate `vector<T>` with a type `T` that does not provide an equality operator `==`.
  85. Otherwise, programmers can guard this postcondition using C++17 `if constexpr` to evaluate the asserted condition only for types `T` that have an equality operator `==` and skip it otherwise.
  86. [footnote
  87. *Rationale:*
  88. __N1962__ and other proposals to add contracts to C++ do not provide a mechanism to selectively disable assertions based on their type requirements.
  89. However, this library provides such a mechanism to allow to program contracts for template code without necessarily adding extra type requirements that would not be present if it was not for the contracts (so compiling the code with and without contracts will not alter the type requirements of the program).
  90. Something similar could be achieved combing C++17 `if constexpr` with __N1962__ or __P0380__ so that contract assertions within template code could be guarded by `if constexpr` statements checking the related type requirements (__N1962__ already allows of `if` statements in contracts under the name of /select assertions/, __P0380__ does not so probably `if` statements should be added to __P0380__ as well).
  91. For example, assuming C++17 `if constexpr` can be used within __P0380__ contracts:
  92. ``
  93. template<typename T>
  94. class vector {
  95. public:
  96. void push_back(T const& value)
  97. [[ensures: if constexpr(boost::has_equal_to<T>::value) back() == value]]
  98. ...
  99. };
  100. ``
  101. ]
  102. For example:
  103. template<typename T>
  104. class vector {
  105. public:
  106. void push_back(T const& value) {
  107. boost::contract::check c = boot::contract::public_function(this)
  108. .postcondition([&] {
  109. // Guard with `if constexpr` for T without `==`.
  110. if constexpr(boost::has_equal_to<T>::value)
  111. BOOST_CONTRACT_ASSERT(back() == value);
  112. })
  113. ;
  114. vect_.push_back(value);
  115. }
  116. /* ... */
  117. More in general, `if constexpr` can be used to guard a mix of both old value copies and contract assertions that introduce specific extra type requirements.
  118. For example, the following `swap` function can be called on any type `T` that is movable but its old value copies and postcondition assertions are evaluated only for types `T` that are also copyable and have an equality operator `==` (see [@../../example/features/if_constexpr.cpp =if_constexpr.cpp=]):
  119. [import ../example/features/if_constexpr.cpp]
  120. [if_constexpr]
  121. [heading No `if constexpr` (no C++17)]
  122. On non-C++17 compilers where `if constexpr` is not available, it is possible to use [funcref boost::contract::condition_if] to skip assertions based on type requirements (even if this code is less readable and more verbose than using `if constexpr`).
  123. For example (see [@../../example/features/condition_if.cpp =condition_if.cpp=]):
  124. [import ../example/features/condition_if.cpp]
  125. [condition_if]
  126. More in general, [funcref boost::contract::condition_if] is used as follow:
  127. boost::contract::condition_if<Pred>(
  128. cond
  129. )
  130. Where `Pred` is a nullary boolean meta-function and `cond` is a nullary boolean functor.
  131. If `Pred::value` is statically evaluated to be `true` at compile-time then `cond()` is called at run-time and its boolean result is returned by the enclosing [funcref boost::contract::condition_if] call.
  132. Otherwise, if `Pred::value` is statically evaluated to be `false` at compile-time then [funcref boost::contract::condition_if] trivially returns `true`.
  133. Therefore, if `cond` is a functor template instantiation (not just a functor) then its call that contains the assertion operations with the extra type requirements (e.g., the equality operator `==`) will not be actually instantiated and compiled unless the compiler determines at compile-time that `Pred::value` is `true` (when used this way, functor templates like `std::equal_to` and C++14 generic lambdas can be used to program `cond`, but C++11 lambdas cannot).
  134. The [funcref boost::contract::condition_if] function template is a special case of the more general [funcref boost::contract::call_if], specifically [funcref boost::contract::condition_if]`<Pred>(cond)` is logically equivalent to [funcref boost::contract::call_if]`<Pred>(cond).else_([] { return true; })`.
  135. [footnote
  136. The internal implementation of [funcref boost::contract::condition_if] is optimized and it does not actually use [funcref boost::contract::call_if].
  137. ]
  138. The [funcref boost::contract::call_if] function template also accepts a number of optional /else-if/ statements and one optional /else/ statement:
  139. boost::contract::call_if<Pred1>(
  140. t1
  141. ).template else_if<Pred2>( // Optional.
  142. t2
  143. )
  144. ... // Optionally, other `else_if` statements.
  145. .else_( // Optional for `void` functors, otherwise required.
  146. e
  147. )
  148. Where `Pred1`, `Pred2`, ... are nullary boolean meta-functions and `t1`, `t2`, ..., `e` are nullary functors.
  149. The return types of the functor calls `t1()`, `t2()`, ..., `e()` must either be all the same (including possibly all `void`) or be of types implicitly convertible into one another.
  150. At run-time [funcref boost::contract::call_if] will call the functor `t1()`, or `t2()`, ..., or `e()` depending on which meta-function `Pred1::value`, `Pred2::value`, ... is statically evaluated to be `true` or `false` at compile-time, and it will return the value returned by the functor being called.
  151. If `t1`, `t2`, ..., `e` are functor template instantiations (not just functors) then their code will only be compiled if the compiler determines they need to be actually called at run-time (so only if the related `Pred1::value`, `Pred2::value`, ... are respectively evaluated to be `true` or `false` at compile-time).
  152. All the `else_if<...>(...)` statements are optional.
  153. The `else_(...)` statement is optional if the functor calls return `void`, otherwise it is required.
  154. In general, [funcref boost::contract::call_if] can be used to program contract assertions that compile and check different functor templates depending on related predicates being statically evaluated to be `true` or `false` at compile-time (but in most cases [funcref boost::contract::condition_if] should be sufficient and less verbose to use).
  155. The [funcref boost::contract::condition_if_c], [funcref boost::contract::call_if_c], and `.else_if_c` function templates work similarly to their counterparts without the `..._c` postfix seen so far, but they take their predicate template parameters as static boolean values instead of nullary boolean meta-functions.
  156. [import ../example/features/call_if_cxx14.cpp]
  157. On compilers that support C++17 `if constexpr` there should be no need to use [funcref boost::contract::condition_if] or [funcref boost::contract::call_if] because `if constexpr` can be used instead (making the code more readable and easier to program).
  158. [footnote
  159. A part from its use within contracts, [funcref boost::contract::call_if] can be used together with C++14 generic lambdas to emulate C++17 `if constexpr` (`boost::hana::if_` and probably other approaches can also be used together with generic lambdas to emulate C++17 `if constexpr` on C++14 compilers).
  160. For example, the following implementation of `myadvance` will compile since C++14 and it is more concise, easier to read and maintain than the usual implementation of `std::advance` that uses tag dispatching (see [@../../example/features/call_if_cxx14.cpp =call_if_cxx14.cpp=]):
  161. [call_if_cxx14]
  162. Of course, since C++17 the implementation that uses `if constexpr` is even more readable and concise:
  163. ``
  164. template<typename Iter, typename Dist>
  165. void myadvance(Iter& i, Dist n) {
  166. if constexpr(is_random_access_iterator<Iter>::value) {
  167. i += n;
  168. } else if constexpr(is_bidirectional_iterator<Iter>::value) {
  169. if(n >= 0) while(n--) ++i;
  170. else while(n++) --i;
  171. } else if constexpr(is_input_iterator<Iter>::value) {
  172. while(n--) ++i;
  173. } else {
  174. static_assert(false, "requires at least input iterator");
  175. }
  176. }
  177. ``
  178. ]
  179. [endsect]
  180. [section Volatile Public Functions]
  181. This library allows to specify a different set of class invariants to check for volatile public functions.
  182. These /volatile class invariants/ are programmed in a public `const volatile` function, named `invariant`, taking no argument, and returning `void` (see [macroref BOOST_CONTRACT_INVARIANT_FUNC] to name the invariant function differently from `invariant` and __Access_Specifiers__ to not have to declare it `public`).
  183. Classes that do no have invariants for their volatile public functions, simply do not declare the `void invariant() const volatile` function.
  184. In general, `const volatile` invariants work the same as `const` invariants (see __Class_Invariants__) with the only difference that `volatile` and `const volatile` functions check `const volatile` invariants while non-`const` (i.e., neither `const` nor `volatile`) and `const` functions check `const` invariants.
  185. A given class can specify any combination of `static`, `const volatile`, and `const` invariant functions (see __Class_Invariants__):
  186. [footnote
  187. *Rationale:*
  188. Constructors and destructors check `const volatile` and `const` invariants in that order because the qualifier that can be applied to more calls is checked first (note that `const volatile` calls can be made on any object while `const` calls cannot be made on `volatile` objects, in that sense the `const volatile` qualifier can be applied to more calls than `const` alone can).
  189. This is consistent with `static` class invariants that are checked even before `const volatile` invariants (the `static` classifier can be applied to even more calls than `const volatile`, in fact an object is not even needed to make static calls).
  190. ]
  191. * Constructors check `static` invariants at entry and exit (even if an exception is thrown), plus `const volatile` and `const` invariants in that order at exit but only if no exception is thrown.
  192. * Destructors check `static` invariants at entry and exit (even if an exception is thrown), plus `const volatile` and `const` invariants in that order at entry (and at exit but only if an exception is thrown, even is destructors should in general never throw in C++).
  193. * Both non-`const` and `const` public functions check `static` and `const` invariants at entry and at exit (even if an exception is thrown).
  194. * Both `volatile` and `const volatile` public functions check `static` and `const volatile` invariants at entry and at exit (even if an exception is thrown).
  195. These rules ensure that volatile class invariants are correctly checked (see __Constructor_Calls__, __Destructor_Calls__, and __Public_Function_Calls__).
  196. For example (see [@../../example/features/volatile.cpp =volatile.cpp=]):
  197. [import ../example/features/volatile.cpp]
  198. [volatile]
  199. This library does not automatically check `const volatile` invariants for non-`volatile` functions.
  200. However, if the contract specifications require it, programmers can explicitly call the `const volatile` invariant function from the `const` invariant function (preferably in that order to be consistent with the order `const volatile` and `const` invariants are checked for constructors and destructors).
  201. That way all public functions, `volatile` or not, will check `const volatile` invariants (while only `const` and non-`const` public functions will check only `const` invariants, correctly so because the `volatile` qualifier shall not be stripped away):
  202. [footnote
  203. *Rationale:*
  204. Note that while all public functions can be made to check `const volatile` invariants, it is never possible to make volatile public functions check `const` non-volatile invariants.
  205. That is because both `const` and `volatile` can always be added but never stripped in C++ (a part from forcefully via `const_cast`) but `const` is always automatically added by this library in order to enforce contract constant-correctness (see __Constant_Correctness__).
  206. That said, it would be too stringent for this library to also automatically add `volatile` and require all functions to check `const volatile` (not just `const`) invariants because only `volatile` members can be accessed from `const volatile` invariants so there could be many `const` (but not `const volatile`) members that are accessible from `const` invariants but not from `const volatile` invariants.
  207. To avoid this confusion, this library has chosen to draw a clear dichotomy between `const` and `const volatile` invariants so that only volatile public functions check `const volatile` invariants and only non-volatile public functions check `const` (but not `const volatile`) invariants.
  208. This is a clear distinction and it should serve most cases.
  209. If programmers need non-volatile public functions to also check `const volatile` invariants, they can explicitly do so by calling the `const volatile` invariant function from the `const` invariant function as shown in this documentation.
  210. ]
  211. class u {
  212. public:
  213. void invariant() const volatile { ... } // Volatile invariants.
  214. void invariant() const {
  215. auto const volatile* cv = this; cv->invariant(); // Call `const volatile` invariant function above.
  216. ... // Other non-volatile invariants.
  217. }
  218. ...
  219. };
  220. (As usual, private and protected functions do not check any invariant, not even when they are `volatile` or `const volatile`, see __Private_and_Protected_Functions__).
  221. [endsect]
  222. [section Move Operations]
  223. As with all public operations of a class, also public move operations should maintain class invariants (see __Stroustrup13__, p. 520).
  224. Specifically, at a minimum C++ requires the following:
  225. * The moved-from object can be copy assigned.
  226. * The moved-from object can be move assigned.
  227. * The moved-from object can be destroyed (if not for any other reason, this requires that class invariants are maintained by move operations because the destructor of the moved-from object requires class invariants to be satisfied at its entry, as always with destructors see __Destructor_Calls__).
  228. Therefore, both the move constructor and the move assignment operator need to maintain the class invariants of the moved-from object so their contracts can be programmed using [funcref boost::contract::constructor] and [funcref boost::contract::public_function] as usual.
  229. For example (see [@../../example/features/move.cpp =move.cpp=]):
  230. [import ../example/features/move.cpp]
  231. [move]
  232. This example assumes that it is possible to call the public function `moved()` on the moved-from object.
  233. [footnote
  234. In this example, the `moved()` function is simple enough that programmers could decide to not even call [funcref boost::contract::public_function] from it for optimization reasons.
  235. However, calling [funcref boost::contract::public_function] from `moved()` has no negative impact, a part from run-time overhead, because this library automatically disables contract checking while checking other contracts (so this call will not cause infinite recursion).
  236. ]
  237. [note
  238. The default move constructor and move assignment operator generated by C++ will not automatically check contracts.
  239. Therefore, unless the move operations are not public or they have no preconditions, no postconditions, and their class has no invariants, programmers should manually define them using [funcref boost::contract::constructor], [classref boost::contract::constructor_precondition], and [funcref boost::contract::public_function] instead of relying on their default implementations generated by C++.
  240. (Same as for all other operations automatically implemented by C++.)
  241. ]
  242. As always, programmers can decide to not program contracts for a given type.
  243. Specifically, they might decide to not program contracts for a class that needs to be moved in order to avoid the run-time overhead of checking contract assertions and to maximize performance (see __Benefits_and_Costs__).
  244. [endsect]
  245. [section Unions]
  246. A C++ `union` cannot have virtual functions, base classes, and cannot be used as a base class thus subcontracting ([classref boost::contract::virtual_], [macroref BOOST_CONTRACT_OVERRIDE], etc.) do not apply to unions.
  247. Also a `union` cannot inherit from [classref boost::contract::constructor_precondition] (because it cannot have base classes), instead [classref boost::contract::constructor_precondition] is used to declare a local object that checks constructor preconditions (at the very beginning of the constructor before old value copies and other contracts, see declaration of `pre` in the example below).
  248. A part from that, this library is used as usual to program contracts for unions.
  249. For example (see [@../../example/features/union.cpp =union.cpp=]):
  250. [import ../example/features/union.cpp]
  251. [union]
  252. [endsect]
  253. [section Assertion Levels]
  254. This library provides three predefined /assertion levels/ that can be used to selectively disable assertions depending on their computational complexity:
  255. [footnote
  256. The assertion levels predefined by this library are similar to the default, audit, and axiom levels from __P0380__.
  257. ]
  258. * [macroref BOOST_CONTRACT_ASSERT] is used to assert conditions that are not computationally expensive, at least compared to the cost of executing the function body.
  259. These assertions are the ones we have seen so far, they are always checked at run-time and they cannot be disabled.
  260. * [macroref BOOST_CONTRACT_ASSERT_AUDIT] is used to assert conditions that are computationally expensive compared to the cost of executing the function body.
  261. These assertions are not checked at run-time unless programmers explicitly define [macroref BOOST_CONTRACT_AUDITS] (undefined by default), but the asserted conditions are always compiled and therefore validated syntactically (even when they are not actually evaluated and checked at run-time).
  262. * [macroref BOOST_CONTRACT_ASSERT_AXIOM] is used to assert conditions that are computationally prohibitive, at least compared to the cost of executing the function body.
  263. These assertions are never evaluated or checked at run-time, but the asserted conditions are always compiled and therefore validated syntactically (so these assertions can serve as formal comments to the code).
  264. In addition, [macroref BOOST_CONTRACT_CHECK_AUDIT] and [macroref BOOST_CONTRACT_CHECK_AXIOM] are similar to [macroref BOOST_CONTRACT_ASSERT_AUDIT] and [macroref BOOST_CONTRACT_ASSERT_AXIOM] but they are used to program audit and axiom levels for implementation checks instead of assertions (see __Implementation_Checks__).
  265. For example, [macroref BOOST_CONTRACT_ASSERT_AUDIT] can be used to program computationally expensive assertions (see [@../../example/features/assertion_level.cpp =assertion_level.cpp=]):
  266. [import ../example/features/assertion_level.cpp]
  267. [assertion_level_audit]
  268. Similarly, [macroref BOOST_CONTRACT_AUDITS] can be used to disable expensive old value copies and related assertions that use them (see [@../../example/features/assertion_level.cpp =assertion_level.cpp=]):
  269. [assertion_level_class_begin]
  270. [assertion_level_audit_old]
  271. [assertion_level_class_end]
  272. The condition passed to [macroref BOOST_CONTRACT_ASSERT_AXIOM] is compiled but not actually evaluated at run-time so this macro can be used to program computationally prohibitive assertions but also assertions that cannot actually be programmed in C++ using functions that are declared but left undefined.
  273. For example, (see [@../../example/features/assertion_level.cpp =assertion_level.cpp=]):
  274. [assertion_level_no_impl]
  275. [assertion_level_class_begin]
  276. [assertion_level_axiom]
  277. [assertion_level_class_end]
  278. In addition to these assertion levels that are predefined by this library, programmers are free to define their own.
  279. For example, the following macro could be used to program and selectively disable assertions that have exponential computational complexity `O(e^n)`:
  280. #ifdef EXPONENTIALLY_COMPLEX_ASSERTIONS
  281. // Following will compile and also evaluate `cond`.
  282. #define ASSERT_EXP(cond) BOOST_CONTRACT_ASSERT(cond)
  283. #else
  284. // Following will compile but never actually evaluate `cond`.
  285. #define ASSERT_EXP(cond) BOOST_CONTRACT_ASSERT(true || (cond))
  286. #endif
  287. ...
  288. ASSERT_EXP(``[^['some-exponentially-complex-boolean-condition]]``);
  289. [endsect]
  290. [section Disable Contract Checking]
  291. Checking contracts adds run-time overhead and can slow down program execution (see __Benefits_and_Costs__).
  292. Therefore, programmers can define any combination of the following macros (`-D` option in Clang and GCC, `/D` option in MSVC, etc.) to instruct this library to not check specific groups of contract conditions at run-time:
  293. * Define [macroref BOOST_CONTRACT_NO_PRECONDITIONS] to not check preconditions.
  294. * Define [macroref BOOST_CONTRACT_NO_POSTCONDITIONS] to not check postconditions.
  295. * Define [macroref BOOST_CONTRACT_NO_EXCEPTS] to not check exception guarantees.
  296. * Define [macroref BOOST_CONTRACT_NO_ENTRY_INVARIANTS] to not check class invariants at call entry.
  297. * Define [macroref BOOST_CONTRACT_NO_EXIT_INVARIANTS] to not check class invariants at call exit.
  298. * Or, define [macroref BOOST_CONTRACT_NO_INVARIANTS] to not check class invariants at both call entry and exit. (This is provided for convenience, it is equivalent to defining both [macroref BOOST_CONTRACT_NO_ENTRY_INVARIANTS] and [macroref BOOST_CONTRACT_NO_EXIT_INVARIANTS].)
  299. * Define [macroref BOOST_CONTRACT_NO_CHECKS] to not evaluate implementation checks.
  300. [note
  301. Old values can be used by both postconditions and exception guarantees so it is necessary to define both [macroref BOOST_CONTRACT_NO_POSTCONDITIONS] and [macroref BOOST_CONTRACT_NO_EXCEPTS] to disable old value copies.
  302. ]
  303. By default, none of these macros are defined so this library checks all contracts.
  304. When these macros are defined by the user, the implementation code of this library is internally optimized to minimize as much as possible any run-time and compile-time overhead associated with checking and compiling contracts (see __Disable_Contract_Compilation__ for techniques to completely remove any run-time and compile-time overheads associated with contract code).
  305. For example, programmers could decide to check all contracts during early development builds, but later check only preconditions and maybe entry invariants for release builds by defining [macroref BOOST_CONTRACT_NO_POSTCONDITIONS], [macroref BOOST_CONTRACT_NO_EXCEPTS], [macroref BOOST_CONTRACT_NO_EXIT_INVARIANTS], and [macroref BOOST_CONTRACT_NO_CHECKS].
  306. [endsect]
  307. [section Disable Contract Compilation (Macro Interface)]
  308. This library provides macros that can be used to completely disable compile-time and run-time overhead introduced by contracts but at the cost of manually programming `#ifndef BOOST_CONTRACT_NO_...` statements around contract code:
  309. * This library defines [macroref BOOST_CONTRACT_NO_CONSTRUCTORS] when contract checking is disabled for constructors.
  310. * This library defines [macroref BOOST_CONTRACT_NO_DESTRUCTORS] when contract checking is disabled for destructors.
  311. * This library defines [macroref BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS] when contract checking is disabled for public functions.
  312. * This library defines [macroref BOOST_CONTRACT_NO_FUNCTIONS] when contract checking is disabled for (non-public and non-member) functions.
  313. * This library defines [macroref BOOST_CONTRACT_NO_OLDS] when old value copies are disabled.
  314. * This library defines [macroref BOOST_CONTRACT_NO_ALL] when all contracts above and also implementation checks (see [macroref BOOST_CONTRACT_NO_CHECKS]) are disabled.
  315. These macros are not configuration macros and they should not be defined directly by programmers (otherwise this library will generate compile-time errors).
  316. Instead, these macros are automatically defined by this library when programmers define [macroref BOOST_CONTRACT_NO_PRECONDITIONS], [macroref BOOST_CONTRACT_NO_POSTCONDITIONS], [macroref BOOST_CONTRACT_NO_EXCEPTS], [macroref BOOST_CONTRACT_NO_INVARIANTS] (or [macroref BOOST_CONTRACT_NO_ENTRY_INVARIANTS] and [macroref BOOST_CONTRACT_NO_EXIT_INVARIANTS]), and [macroref BOOST_CONTRACT_NO_CHECKS] (see __Disable_Contract_Checking__).
  317. Alternatively, this library provides a macro-based interface defined in [headerref boost/contract_macro.hpp] that can also be used to completely disable compile-time and run-time overheads introduced by contracts but without the burden of manually writing the `#ifndef BOOST_CONTRACT_NO_...` statements.
  318. For example, the following code shows how to use both the [headerref boost/contract_macro.hpp] macro interface and the `#ifndef BOOST_CONTRACT_NO_...` statements to completely disable compile-time and run-time overheads for non-member function contracts (see [@../../example/features/ifdef_macro.cpp =ifdef_macro.cpp=] and [@../../example/features/ifdef.cpp =ifdef.cpp=]):
  319. [import ../example/features/ifdef_macro.cpp]
  320. [import ../example/features/ifdef.cpp]
  321. [table
  322. [ [Macro Interface] [`#ifndef BOOST_CONTRACT_NO_...` Statements] ]
  323. [ [[ifdef_macro_function]] [[ifdef_function]] ]
  324. ]
  325. The same can be done to disable contract code complication for private and protected functions.
  326. The [macroref BOOST_CONTRACT_OLD_PTR_IF_COPYABLE] macro is provided to handle non-copyable old value types (similar to [classref boost::contract::old_ptr_if_copyable]).
  327. For constructors, destructors, and public functions the [headerref boost/contract_macro.hpp] macro interface and the `#ifndef BOOST_CONTRACT_NO_...` statements can be used as follow (see [@../../example/features/ifdef_macro.cpp =ifdef_macro.cpp=] and [@../../example/features/ifdef.cpp =ifdef.cpp=]):
  328. [table
  329. [ [Macro Interface] [`#ifndef BOOST_CONTRACT_NO_...` Statements] ]
  330. [ [[ifdef_macro_class]] [[ifdef_class]] ]
  331. ]
  332. Static and volatile class invariants can be programmed using [macroref BOOST_CONTRACT_STATIC_INVARIANT] and [macroref BOOST_CONTRACT_INVARIANT_VOLATILE] respectively (these macros expand code equivalent to the `static void BOOST_CONTRACT_STATIC_INVARIANT_FUNC()` and `void BOOST_CONTRACT_INVARIANT_FUNC() const volatile` functions).
  333. The [headerref boost/contract_macro.hpp] macro interface is usually preferred because more concise and easier to use than programming `#ifndef BOOST_CONTRACT_NO_...` statements by hand.
  334. However, C++ macros expand on a single line of code and that can make compiler errors less useful when using this macro interface plus all contract assertions within a given set of preconditions, postconditions, exception guarantees, and class invariants will list the same line number in error messages when assertions fail at run-time (but error messages still list the assertion code and that should still allow programmers to identify the specific assertion that failed).
  335. Finally, the macro interface leaves a bit of contract decorations in the code but that should add no measurable compile-time or run-time overhead (specifically, extra [classref boost::contract::virtual_]`*` parameters, calls to [classref boost::contract::constructor_precondition] default constructor which does nothing, [macroref BOOST_CONTRACT_BASE_TYPES] `typedef`s, and [classref boost::contract::access] friendships are left in user code even when contracts are disabled unless `#ifndef BOOST_CONTRACT_NO_...` statements are used).
  336. Disabling contract as shown in __Disable_Contract_Checking__ leaves the overhead of compiling contract code plus some small run-time overhead due to the initialization of old value pointers (even if those will be all null and no old value will be actually copied), the calls to the contract functions used to initialize [classref boost::contract::check] and [classref boost::contract::constructor_precondition] (even if those calls will be internally optimized by this library to essentially do nothing), etc.
  337. For truly performance critical code for which even such small run-time overhead might not be acceptable, the macro interface (or the `#ifndef BOOST_CONTRACT_NO_...` statements) can be used to completely disable compile-time and run-time overheads of contracts.
  338. However, for such performance critical code even the overhead of checking simple preconditions might be too much so it might be best to not program contracts at all.
  339. Usually, if the overhead of checking preconditions and other assertions is already considered acceptable for an application then the compile-time overhead of contracts should not represent an issue and it should be sufficient to disable contract checking at run-time as indicated in __Disable_Contract_Checking__ (without a real need to use the [headerref boost/contract_macro.hpp] macro interface or the `#ifndef BOOST_CONTRACT_NO_...` statements in most cases).
  340. [endsect]
  341. [section Separate Body Implementation]
  342. Contracts are part of the program specifications and not of its implementation (see __Specifications_vs_Implementation__).
  343. However, this library uses function definitions to program contracts so contract code appears together with the function implementation code.
  344. This is not ideal (even if contracts programmed using this library will always appear at the very beginning of the function definition so programmers will easily be able to distinguish contract code from the rest of the function implementation code so this might not be real limitation in practise).
  345. In some cases, it might be desirable to completely separate the contract code from the function implementation code.
  346. For example, this could be necessary for software that ships only header files and compiled object files to its users.
  347. If contracts are programmed in function definitions that are compiled in the object files, users will not be able to see the contract code to understand semantics and usage of the functions (again, this might not be a real problem in practice for example if contracts are already somehow extracted from the source code by some tool and presented as part of the documentation of the shipped software).
  348. In any case, when it is truly important to separate contracts from function implementation code, function implementations can be programmed in extra /body functions/ (here named `..._body`, but any other naming scheme could be used) that are compiled in object files.
  349. Function definitions that remain in header files instead will contain just contract code followed by calls to the extra body functions.
  350. This technique allows to keep the contract code in header files while separating the implementation code to source and object files.
  351. However, this adds the overhead of manually programming an extra function declaration for each body function (plus the limitation that constructor member initialization lists must be programmed in header files because that is where constructors need to be defined to list constructor contract code).
  352. [footnote
  353. When used as default parameter values, lambda functions allow to program code statements within function declarations.
  354. However, these lambadas cannot be effectively used to program contracts in function declarations instead of definitions.
  355. That is because the C++11 standard does not allow lambdas in function declarations to capture any variable (for the good reason that it is not at all obvious how to correctly define the semantics of such captures).
  356. For example, the following code is not valid C++ and it does not compile:
  357. ``
  358. // Specifications (in declaration).
  359. int inc(int& x,
  360. // Error: Lambdas in default parameters cannot capture `this`, `x`, or any other variable.
  361. std::function<void ()> pre = [&] {
  362. BOOST_CONTRACT_ASSERT(x < std::numeric_limits<int>::max());
  363. },
  364. std::function<void (int const&, boost::contract::old_ptr<int> const&)> post
  365. = [&] (int const& result, boost::contract::old_ptr<int> const& old_x)
  366. {
  367. BOOST_CONTRACT_ASSERT(x == *old_x + 1);
  368. BOOST_CONTRACT_ASSERT(result == *old_x);
  369. }
  370. );
  371. // Implementation (in definition).
  372. int inc(int& x,
  373. std::function<void ()> pre,
  374. std::function<void (int const&, boost::contract::old_ptr<int> const&)> post
  375. ) {
  376. int result;
  377. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  378. boost::contract::check c = boost::contract::function()
  379. .precondition(pre)
  380. .postcondition(std::bind(post, std::cref(result), std::cref(old_x)))
  381. ;
  382. return result = x++; // Function body.
  383. }
  384. ``
  385. In any case, even if the above code compiled, it would require significant boiler-plate code to bind return and old values.
  386. ]
  387. For example, the following header file only contains function declarations, contract code, and constructor member initializations, but it does not contain the code implementing the function bodies (see [@../../example/features/separate_body.hpp =separate_body.hpp=]):
  388. [import ../example/features/separate_body.hpp]
  389. [separate_body_hpp]
  390. Instead, the function bodies are implemented in a separate source file (see [@../../example/features/separate_body.cpp =separate_body.cpp=]):
  391. [import ../example/features/separate_body.cpp]
  392. [separate_body_cpp]
  393. The same technique can be used for non-member, private, and protected functions, etc.
  394. [note
  395. When contracts are programmed only in =.cpp= files and also all this library headers are `#include`d only from =.cpp= files, then these =.cpp= files can be compiled disabling specific contract checking (for example, [macroref BOOST_CONTRACT_NO_POSTCONDITIONS], [macroref BOOST_CONTRACT_NO_EXCEPTS], and [macroref BOOST_CONTRACT_NO_EXIT_INVARIANTS], see __Disable_Contract_Checking__).
  396. Then the code in these =.cpp= files will always have such contract checking disabled even when linked to some other user code that might have been compiled with a different set of contracts disabled (i.e., a different set of `BOOST_CONTRACT_NO_...` macros defined).
  397. This technique might be useful to ship compiled object files (e.g., for a library) that will never check some contracts (e.g., postconditions, exception guarantees, and exit invariants) regardless of the definition of the `BOOST_CONTRACT_NO_...` macros used to compile code that links against the shipped object files.
  398. On the flip side, if contracts are programmed only in header files (e.g., using extra `..._body` functions as shown in this section) and this library headers are `#include`d only in these header files that are being shipped, then end users can enable or disables contract checking of the shipped code by defining the `BOOST_CONTRACT_NO_...` macros when they compile the shipped header files as part of their code.
  399. This technique might be useful in other situations when programmers that ship code want to leave it up the their end users to decide which contracts of the shipped code should be checked at run-time.
  400. ]
  401. [endsect]
  402. [section No Lambda Functions (No C++11)]
  403. This section shows how to use this library without C++11 lambda functions.
  404. This has some advantages:
  405. * It allows to use this library on compilers that do not support C++11 lambda functions (essentially most C++03 compilers with adequate support for SFINAE can be used in that case, see __No_Macros__ to also avoid using variadic macros).
  406. [footnote
  407. Alternatively, on compilers that do not support C++11 lambda functions, [@http://www.boost.org/doc/libs/release/libs/local_function/doc/html/index.html Boost.LocalFunction] could be used to program the contract functors still within the function definitions (for example, see [@../../example/features/no_lambdas_local_func.cpp =no_lambda_local_func.cpp=]).
  408. In general, such a code is less verbose than the example shown in this section that uses contract functions programmed outside of the original function definitions (about 30% less lines of code) but the contract code is hard to read.
  409. Other libraries could also be used to program the contract functors without C++11 lambda functions (Boost.Lambda, Boost.Fusion, etc.) but again all these techniques will result in contract code either more verbose, or harder to read and maintain than the code that uses C++11 lambda functions.
  410. ]
  411. * Contract functions (i.e., the `..._precondition`, `..._old`, and `..._postcondition` functions in the example below) can be programmed to fully enforce constant-correctness and other contract requirements at compile-time (see __Constant_Correctness__).
  412. [footnote
  413. If C++ allowed lambda functions to capture variables by constant reference (for example allowing a syntax like this `[const&] { ... }` and `[const& `[^['variable-name]]`] { ... }`, see [@https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/0UKQw9eo3N0]) also lambdas could be used to program contract functors that fully enforce __Constant_Correctness__ at compile-time.
  414. Note that C++11 lambdas allow to capture variables by value (using `[=] { ... }` and `[`[^['variable-name]]`] { ... }`) and these value captures are `const` (unless the lambda is explicitly declared `mutable`) but they are not suitable to program postconditions and exception guarantees using this library (because those require capturing by reference, see __Postconditions__ and __Exception_Guarantees__), plus they introduce a copy of the captured value that might be too expensive in general and therefore not suitable for preconditions either.
  415. ]
  416. * Code of the contract functions is separated from function body implementations (see __Separate_Body_Implementation__).
  417. However, not using C++11 lambda functions comes at the significant cost of having to manually program the extra contract functions and related boiler-plate code.
  418. For example, the header file (see [@../../example/features/no_lambdas.hpp =no_lambdas.hpp=]):
  419. [import ../example/features/no_lambdas.hpp]
  420. [no_lambdas_hpp]
  421. And, the source file (see [@../../example/features/no_lambdas.cpp =no_lambdas.cpp=]):
  422. [import ../example/features/no_lambdas.cpp]
  423. [no_lambdas_cpp]
  424. If programmers also want to fully enforce all contract programming constant-correctness requirements at compile-time, they should follow these rules when programming the contract functions (see __Constant_Correctness__):
  425. * Precondition functions (i.e., the `..._precondition` functions in the example above) can take their arguments either by `const` value or by `const&`, and when they are member functions they should be either `static` or `const` functions.
  426. * Postcondition functions (i.e., the `..._postcondition` functions in the example above) should take their arguments by `const&`, and when they are member functions they should be either `static` or `const` functions.
  427. * Similarly, exception guarantee functions (not shown in the example above) should take their arguments by `const&`, and when they are member functions they should be either `static` or `const` functions.
  428. * Old value functions (i.e., the `..._old` functions in the example above) should take their arguments by `const&` a part from old value pointers that should be taken by `&` (so only old value pointers can be modified), and when they are member functions they should be either `static` or `const` functions.
  429. * For constructors: Precondition, old value, and exception guarantee functions should be `static` (because there is no valid object `this` if the constructor body does not run successfully, see __Constructor_Calls__).
  430. * For destructors: Postcondition functions should be `static` (because there is no valid object `this` after the destructor body runs successfully, but exception guarantee functions do not have to be `static` since the object `this` is still valid because the destructor body did not run successfully, see __Destructor_Calls__).
  431. Note that the extra contract functions also allow to keep the contract code in the header file while all function bodies are implemented in a separate source file (including the constructor member initialization list, that could not be done with the techniques shown in __Separate_Body_Implementation__).
  432. [footnote
  433. In this example, `bind` was used to generate nullary functors from the contract functions.
  434. As always with `bind`, `cref` and `ref` must be used to bind arguments by `const&` and `&` respectively, plus it might be necessary to explicitly `static_cast` the function pointer passed to `bind` for overloaded functions.
  435. ]
  436. Also note that the contract functions can always be declared `private` if programmers need to exactly control the public members of the class (this was not done in this example only for brevity).
  437. The authors think this library is most useful when used together with C++11 lambda functions (because of the large amount of boiler-plate code required when C++11 lambdas are not used as also shown by the example above).
  438. [endsect]
  439. [section No Macros (and No Variadic Macros)]
  440. It is possible to specify contracts without using most of the macros provided by this library and programming the related code manually instead (the only macros that cannot be programmed manually are [macroref BOOST_CONTRACT_OVERRIDE], [macroref BOOST_CONTRACT_OVERRIDES], and [macroref BOOST_CONTRACT_NAMED_OVERRIDE]).
  441. [note
  442. Some of this library macros are variadic macros, others are not (see below).
  443. Variadic macros were officially added to the language in C++11 but most compilers have been supporting them as an extension for a long time, plus all compilers that support C++11 lambda functions should also support C++11 variadic macros (and this library might rarely be used without the convenience of C++11 lambda functions, see __No_Lambda_Functions__).
  444. [footnote
  445. Compilation times of this library were measured to be comparable between compilers that support variadic macros and compilers that do not.
  446. ]
  447. Therefore, the rest of this section can be considered mainly a curiosity because programmers should seldom, if ever, need to use this library without using its macros.
  448. ]
  449. [heading Overrides]
  450. As shown in __Public_Function_Overrides__ and __Named_Overrides__, this library provides the [macroref BOOST_CONTRACT_OVERRIDE] and [macroref BOOST_CONTRACT_NAMED_OVERRIDE] macros to program contracts for overriding public functions (see [macroref BOOST_CONTRACT_MAX_ARGS] for compilers that do not support variadic templates).
  451. [footnote
  452. *Rationale:*
  453. The [macroref BOOST_CONTRACT_MAX_ARGS] macro is named after `BOOST_FUNCTION_MAX_ARGS`.
  454. ]
  455. These macro cannot be programmed manually but they are not variadic macros (so programmers should be able to use them on any C++ compiler with a sound support for SFINAE).
  456. [footnote
  457. *Rationale:*
  458. These macros expand to SFINAE-based introspection template code that are too complex to be programmed manually by users (that remains the case even if C++14 generic lambdas were to be used here).
  459. On a related note, in theory using C++14 generic lambdas, the [macroref BOOST_CONTRACT_OVERRIDE] macro could be re-implemented in a way that can be expanded at function scope, instead of class scope (but there is not really a need to do that).
  460. ]
  461. The [macroref BOOST_CONTRACT_OVERRIDES] macro is a variadic macro instead but programmes can manually repeat the non-variadic macro [macroref BOOST_CONTRACT_OVERRIDE] for each overriding public function name on compilers that do not support variadic macros.
  462. [heading Assertions (Not Variadic)]
  463. As shown in __Preconditions__, __Postconditions__, __Exception_Guarantees__, __Class_Invariants__, etc. this library provides the [macroref BOOST_CONTRACT_ASSERT] macro to assert contract conditions.
  464. This is not a variadic macro and programmers should be able to use it on all C++ compilers.
  465. In any case, the invocation `BOOST_CONTRACT_ASSERT(`[^['cond]]`)` simply expands to code equivalent to the following:
  466. [footnote
  467. *Rationale:*
  468. There is no need for the code expanded by [macroref BOOST_CONTRACT_ASSERT] to also use C++11 `__func__`.
  469. That is because `__func__` will always expand to the name `operator()` of the functor used to program the contract assertions (e.g., the internal name the compiler assigns to lambda functions) and it will not expand to the name of the actual function enclosing the contract declaration.
  470. ]
  471. if(!(``[^['cond]]``)) {
  472. throw boost::contract::assertion_failure(__FILE__, __LINE__,
  473. BOOST_PP_STRINGIZE(``[^['cond]]``));
  474. }
  475. In fact, this library considers any exception thrown from within preconditions, postconditions, exception guarantees, and class invariants as a contract failure and reports it calling the related contract failure handler ([funcref boost::contract::precondition_failure], etc.).
  476. If there is a need for it, programmers can always program contract assertions that throw specific user-defined exceptions as follow (see __Throw_on_Failures__):
  477. if(!``[^['cond]]``) throw ``[^['exception-object]]``;
  478. However, using [macroref BOOST_CONTRACT_ASSERT] is convenient because it always allows this library to show an informative message in case of assertion failure containing the assertion code, file name, line number, etc.
  479. As shown in __Assertion_Levels__, this library pre-defines [macroref BOOST_CONTRACT_ASSERT_AUDIT] and [macroref BOOST_CONTRACT_ASSERT_AXIOM] assertion levels.
  480. These macros are not variadic macros and programmers should be able to use them on all C++ compilers.
  481. In any case, their implementations are equivalent to the following:
  482. #ifdef BOOST_CONTRACT_AUDITS
  483. #define BOOST_CONTRACT_ASSERT_AUDIT(``[^['cond]]``) \
  484. BOOST_CONTRACT_ASSERT(``[^['cond]]``)
  485. #else
  486. #define BOOST_CONTRACT_ASSERT_AUDIT(``[^['cond]]``) \
  487. BOOST_CONTRACT_ASSERT(true || (``[^['cond]]``))
  488. #endif
  489. #define BOOST_CONTRACT_ASSERT_AXIOM(``[^['cond]]``) \
  490. BOOST_CONTRACT_ASSERT(true || (``[^['cond]]``))
  491. [heading Base Types (Variadic)]
  492. As shown in __Base_Classes__, this library provides the [macroref BOOST_CONTRACT_BASE_TYPES] variadic macro to declare the `base_types` member type that will expand to the list of all public bases for a derived class.
  493. Programmers can also declare `base_types` without using [macroref BOOST_CONTRACT_BASE_TYPES] at the cost of writing a bit more code and increase maintenance efforts.
  494. For example (see [@../../example/features/base_types_no_macro.cpp =base_types_no_macro.cpp=]):
  495. [import ../example/features/base_types_no_macro.cpp]
  496. [base_types_no_macro]
  497. The `base_types` member type must be a `boost::mpl::vector` which must list /all and only/ `public` base classes (because only public bases subcontract, see __Function_Calls__), and in the same order these public base classes appear in the derived class inheritance list.
  498. If the [macroref BOOST_CONTRACT_BASE_TYPES] macro is not used, it is the responsibility of the programmers to maintain the correct list of bases in the `boost::mpl::vector` each time the derived class inheritance list changes (this might significantly complicate maintenance).
  499. In general, it is recommended to use the [macroref BOOST_CONTRACT_BASE_TYPES] macro whenever possible.
  500. [heading Old Values (Variadic)]
  501. As shown in __Old_Values__, this library provides the [macroref BOOST_CONTRACT_OLDOF] variadic macro to assign old value copies.
  502. Programmers can also assign old values without using [macroref BOOST_CONTRACT_OLDOF] at the cost of writing a bit more code manually.
  503. For example (see [@../../example/features/old_no_macro.cpp =old_no_macro.cpp=]):
  504. [import ../example/features/old_no_macro.cpp]
  505. [old_no_macro]
  506. The ternary operator `boost::contract::copy_old(v) ? size() : boost::contract::null_old()` must be used here to avoid evaluating and copying the old value expression `size()` when [funcref boost::contract::copy_old] returns `false` (because old values are not being copied when postcondition and exception guarantee checking is disabled at run-time, an overridden virtual function call is not checking postconditions or exception guarantees yet, etc.).
  507. The enclosing [funcref boost::contract::make_old] copies the old value expression and creates an old value pointer.
  508. Otherwise, [funcref boost::contract::null_old] indicates that a null old value pointer should be created.
  509. The [funcref boost::contract::make_old] and [funcref boost::contract::copy_old] functions are used exactly as shown above but without the extra `v` parameter when they are called from within non-virtual functions (see __Public_Function_Overrides__).
  510. The old value pointer returned by [funcref boost::contract::make_old] can be assigned to either [classref boost::contract::old_ptr] or [classref boost::contract::old_ptr_if_copyable] (see __Old_Value_Requirements__).
  511. In general, it is recommended to use the [macroref BOOST_CONTRACT_OLDOF] macro whenever possible.
  512. [heading Macro Interface (Variadic)]
  513. Almost all macros defined in [headerref boost/contract_macro.hpp] are variadic macros.
  514. On compilers that do not support variadic macros, programmers can manually disable contract code compilation using `#ifndef BOOST_CONTRACT_NO_...` statements as shown in __Disable_Contract_Compilation__.
  515. [endsect]
  516. [endsect]