Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Macro BOOST_CONTRACT_CONSTRUCTOR

BOOST_CONTRACT_CONSTRUCTOR — Program contracts that can be completely disabled at compile-time for constructors.

Synopsis

// In header: <boost/contract_macro.hpp>

BOOST_CONTRACT_CONSTRUCTOR(...)

Description

This is used together with BOOST_CONTRACT_POSTCONDITION, BOOST_CONTRACT_EXCEPT, and BOOST_CONTRACT_OLD to specify postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for constructors (see BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION to specify preconditions for constructors):

class u {
    friend class boost::contract::access;

    BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile).
        BOOST_CONTRACT_ASSERT(...);
        ...
    })

public:
    u(...) {
        BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
        BOOST_CONTRACT_CONSTRUCTOR(this)
            // No `PRECONDITION` (use `CONSTRUCTOR_PRECONDITION` if needed).
            BOOST_CONTRACT_OLD([&] { // Optional.
                old_var = BOOST_CONTRACT_OLDOF(old_epxr);
                ...
            })
            BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
            BOOST_CONTRACT_EXCEPT([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
        ; // Trailing `;` is required.

        ... // Constructor body.
    }

    ...
};

For optimization, this can be omitted for constructors that do not have postconditions and exception guarantees, within classes that have no invariants.

BOOST_CONTRACT_CONSTRUCTOR(obj) expands to code equivalent to the following (note that no code is generated when BOOST_CONTRACT_NO_CONSTRUCTORS is defined):

#ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
    boost::contract::check internal_var =
            boost::contract::constructor(obj)
#endif

Where:

  • obj is the object this from the scope of the enclosing constructor declaring the contract. Constructors check all class invariants, including static and volatile invariants (see BOOST_CONTRACT_INVARIANT, BOOST_CONTRACT_STATIC_INVARIANT, and BOOST_CONTRACT_INVARIANT_VOLATILE). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)

  • internal_var is a variable name internally generated by this library (this name is unique but only on different line numbers so this macro cannot be expanded multiple times on the same line).

See Also:

Disable Contract Compilation, Constructors


PrevUpHomeNext