| Home | Libraries | People | FAQ | More

## Function template destructor

boost::contract::destructor — Program contracts for destructors.

## Synopsis

    // In header: <boost/contract/destructor.hpp>

    template<typename Class>
      specify_old_postcondition_except destructor(Class * obj);

## Description

This is used to specify postconditions, exception guarantees, old value copies at body, and check class invariants for destructors (destructors cannot have preconditions, see Destructor Calls):

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

        void invariant() const { // Optional (as for static and volatile).
            BOOST_CONTRACT_ASSERT(...);
            ...
        }

    public:
        ~u() {
            boost::contract::old_ptr<old_type> old_var;
            boost::contract::check c = boost::contract::destructor(this)
                // No `.precondition` (destructors have no preconditions).
                .old([&] { // Optional.
                    old_var = BOOST_CONTRACT_OLDOF(old_expr);
                    ...
                })
                .postcondition([&] { // Optional.
                    BOOST_CONTRACT_ASSERT(...);
                    ...
                })
                .except([&] { // Optional.
                    BOOST_CONTRACT_ASSERT(...);
                    ...
                })
            ;

            ... // Destructor body.
        }

        ...
    };

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

See Also: Destructors

Parameters: |
`obj` | 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).
Template Parameters: |
`Class` | The type of the class containing the destructor declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)
Returns: | The result of this function must be assigned to a variable of type `boost::contract::check` declared explicitly (i.e., without using C++11 `auto` declarations) and locally just before the code of the destructor body (otherwise this library will generate a run-time error, see `BOOST_CONTRACT_ON_MISSING_CHECK_DECL`).