| Home | Libraries | People | FAQ | More

## Function template public_function

boost::contract::public_function — Program contracts for static public functions.

## Synopsis

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

    template<typename Class>
      specify_precondition_old_postcondition_except public_function();

## Description

This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check static class invariants for static public functions:

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

        static void static_invariant() { // Optional (as for non-static).
            BOOST_CONTRACT_ASSERT(...);
            ...
        }

    public:
        static void f(...) {
            boost::contract::old_ptr<old_type> old_var;
            boost::contract::check c = boost::contract::public_function<u>()
                .precondition([&] { // Optional.
                    BOOST_CONTRACT_ASSERT(...);
                    ...
                })
                .old([&] { // Optional.
                    old_var = BOOST_CONTRACT_OLDOF(old_expr);
                    ...
                })
                .postcondition([&] { // Optional.
                    BOOST_CONTRACT_ASSERT(...);
                    ...
                })
                .except([&] { // Optional.
                    BOOST_CONTRACT_ASSERT(...);
                    ...
                })
            ;

            ... // Function body.
        }

        ...
    };

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

See Also:

Static Public Functions

Template Parameters:

`Class` | The type of the class containing the static public function declaring the contract. This template parameter must be explicitly specified for static public functions (because they have no object `this` so there is no function argument from which this type template parameter can be automatically deduced by C++).

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 static public function body (otherwise this library will generate a run-time error, see `BOOST_CONTRACT_ON_MISSING_CHECK_DECL`).