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 a snapshot of the master branch, built from commit 97da996172.
PrevUpHomeNext

Macro BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION

BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION — Program contracts that can be completely disabled at compile-time for static public functions.

Synopsis

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

BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(...)

Description

This is used together with BOOST_CONTRACT_PRECONDITION, BOOST_CONTRACT_POSTCONDITION, BOOST_CONTRACT_EXCEPT, and BOOST_CONTRACT_OLD to specify preconditions, postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for static public functions:

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

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

public:
    static void f(...) {
        BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
        BOOST_CONTRACT_PUBLIC_FUNCTION(u)
            BOOST_CONTRACT_PRECONDITION([&] { // Optional.
                BOOST_CONTRACT_ASSERT(...);
                ...
            })
            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.

        ... // 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.

BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(class_type) expands to code equivalent to the following (note that no code is generated when BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS is defined):

#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
    boost::contract::check internal_var =
            boost::contract::public_function<class_type>()
#endif

Where:

  • class_type is the type of the class containing the static public function declaring the contract. (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, Static Public Functions


PrevUpHomeNext