Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Macro BOOST_CONTRACT_DESTRUCTOR

BOOST_CONTRACT_DESTRUCTOR — Program contracts that can be completely disabled at compile-time for destructors.

Synopsis

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

BOOST_CONTRACT_DESTRUCTOR(...)

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 destructors (destructors cannot have preconditions, see Destructor Calls):

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_DESTRUCTOR(this)
            // No `PRECONDITION` (destructors have no preconditions).
            BOOST_CONTRACT_OLD([&] { // Optional.
                old_var = BOOST_CONTRACT_OLDOF(old_expr);
                ...
            })
            BOOST_CONTRACT_POSTCONDITION([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
            BOOST_CONTRACT_EXCEPT([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
        ; // Trailing `;` is required.

        ... // Destructor body.
    }
    
    ...
};

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

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

#ifndef BOOST_CONTRACT_NO_DESTRUCTORS
    boost::contract::check internal_var =
            boost::contract::destructor(obj)
#endif

Where:

  • obj is the object this from the scope of the enclosing destructor declaring the contract. Destructors check all class invariants, including static and volatile invariants (see Class Invariants and Volatile Public Functions). (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, Destructors


PrevUpHomeNext