Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Class template constructor_precondition

boost::contract::constructor_precondition — Program preconditions for constructors.

Synopsis

// 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 &);
};

Description

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
{
    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.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).

This class cannot be used this way in a union because unions cannot have base classes in C++. Instead, this class is used in a union to declare a local object within the constructor definition just before boost::contract::constructor is used (see Unions).

See Also:

Constructors

Template Parameters

  1. typename Class

    The class type of the constructor for which preconditions are being programmed.

constructor_precondition public construct/copy/destruct

  1. constructor_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] Note

    The implementation of this library is optimized so that calling this default constructor should amount to negligible compile-time and run-time overheads (likely to be optimized away completely by most compilers).

  2. template<typename F> explicit constructor_precondition(F const & f);
    Construct this object specifying constructor preconditions.

    Parameters:

    f

    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.


PrevUpHomeNext