...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::contract::constructor_precondition — Program preconditions for constructors.
// In header: <boost/contract/core/constructor_precondition.hpp> template<typename Class> class constructor_precondition { public: // construct/copy/destruct constructor_precondition(); template<typename F> explicit constructor_precondition(F const &); };
This class must be the very first base of the class declaring the constructor for which preconditions are programmed (that way constructor arguments can be checked by preconditions even before they are used to initialize other base classes):
class u #define BASES private boost::contract::constructor_precondition<u>, \ public b : BASES { ... #undef BASES public: explicit u(unsigned x) : boost::contract::constructor_precondition<u>([&] { BOOST_CONTRACT_ASSERT(x != 0); ... }), b(1.0 / float(x)) { ... } ... };
User-defined classes should inherit privately from this class (to not alter the public interface of user-defined classes). In addition, this class should never be declared as a virtual base (because virtual bases are initialized only once across the entire inheritance hierarchy preventing preconditions of other base classes from being checked).
Unions cannot have base classes in C++ so this class can be used to declare a local object within the constructor definition just before boost::contract::constructor
is used (see Unions).
See Also:
typename Class
The class type of the constructor for which preconditions are being programmed.
constructor_precondition
public
construct/copy/destructconstructor_precondition();Construct this object without specifying constructor preconditions.
This is implicitly called for those constructors of the contracted class that do not specify preconditions.
Note | |
---|---|
Calling this default constructor should amount to negligible compile-time and run-time overheads (likely to be optimized away completely by most compilers). |
template<typename F> explicit constructor_precondition(F const & f);Construct this object specifying constructor preconditions.
Parameters: |
|