![]() |
Home | Libraries | People | FAQ | More |
BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION — Program preconditions that can be disabled at compile-time for constructors.
// In header: <boost/contract_macro.hpp>
BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(...)
This is used together with BOOST_CONTRACT_CONSTRUCTOR
to specify contracts for constructors. Constructors that do not have preconditions do not use this macro. When at least one of the class constructors uses this macro, boost::contract::constructor_precondition
must be the first and private base of the class declaring the constructor for which preconditions are programmed:
class u #define BASES private boost::contract::constructor_precondition<u>, \ public b : BASES { friend class boost::contract::access; typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; #undef BASES ... public: explicit u(unsigned x) : BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(u)([&] { BOOST_CONTRACT_ASSERT(x != 0); }), b(1 / x) { ... } ... };
BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(class_type)(f)
expands to code equivalent to the following (note that when BOOST_CONTRACT_NO_PRECONDITIONS
is defined, this macro trivially expands to a default constructor call that is internally implemented to do nothing so this should have minimal to no overhead):
// Guarded only by NO_PRECONDITIONS (and not also by NO_CONSTRUCTORS) // because for constructor's preconditions (not for postconditions, etc.). #ifndef BOOST_CONTRACT_NO_PRECONDITIONS boost::contract::constructor_precondition<class_type>(f) #else // No-op call (likely optimized away, minimal to no overhead). boost::contract::constructor_precondition<class_type>() #endif
Where:
class_type
is the type of the class containing the constructor for which preconditions are being programmed. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)
f
is the nullary functor called by this library to check constructor preconditions f()
. Assertions within this functor call are usually programmed using BOOST_CONTRACT_ASSERT
, but any exception thrown by a call to this functor indicates a contract failure (and will result in this library calling boost::contract::precondition_failure
). This functor should capture variables by (constant) value, or better by (constant) reference to avoid extra copies. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)
See Also:
Disable Contract Compilation, Constructors