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

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Function template public_function

boost::contract::public_function — Program contracts for non-void virtual public functions that do not override.

Synopsis

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


template<typename VirtualResult, typename Class> 
  specify_precondition_old_postcondition_except< VirtualResult > 
  public_function(virtual_ * v, VirtualResult & r, Class * obj);

Description

This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public functions that are virtual, do not override, and do not return void:

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

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

public:
    t f(..., boost::contract::virtual_* v = 0) {
        t result;
        boost::contract::old_ptr<old_type> old_var;
        boost::contract::check c = boost::contract::public_function(
                v, result, this)
            .precondition([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
            .old([&] { // Optional.
                old_var = BOOST_CONTRACT_OLDOF(v, old_expr);
                ...
            })
            .postcondition([&] (t const& result) { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
            .except([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
        ;

        ... // Function body (use `return result = return_expr`).
    }
    
    ...
};

A virtual public function should always call boost::contract::public_function otherwise this library will not be able to correctly use it for subcontracting.

See Also:

Virtual Public Functions

Parameters:

obj

The object this from the scope of the enclosing virtual public function declaring the contract. This object might be mutable, const, volatile, or const volatile depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see Volatile Public Functions).

r

A reference to the return value of the enclosing virtual public function declaring the contract. This is usually a local variable declared by the enclosing virtual public function just before the contract, but programmers must set it to the actual value being returned by the function at each return statement.

v

The trailing parameter of type boost::contract::virtual_* and default value 0 from the enclosing virtual public function.

Template Parameters:

Class

The type of the class containing the virtual public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)

VirtualResult

This type must be the same as, or compatible with, the return type of the enclosing virtual public function declaring the contract (this library might not be able to generate a compile-time error if these types mismatch, but in general that will cause run-time errors or undefined behaviour). Alternatively, boost::optional<return-type> can also be used (see Optional Return Values). (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 explicitly assigned to a variable of type boost::contract::check declared locally just before the code of the public function body (otherwise this library will generate a run-time error, see BOOST_CONTRACT_ON_MISSING_CHECK_DECL).


PrevUpHomeNext