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 to view this page for the latest version.
PrevUpHomeNext

Macro BOOST_CONTRACT_FUNCTION

BOOST_CONTRACT_FUNCTION — Program contracts that can be completely disabled at compile-time for (non-public) functions.

Synopsis

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

BOOST_CONTRACT_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 (non-public) functions:

void f(...) {
    BOOST_CONTRACT_OLD_PTR(old_type)(old_var);
    BOOST_CONTRACT_FUNCTION()
        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(...);
            ...
        })
    ;

    ... // Function body.
}

This can be used to program contracts for non-member functions but also for private and protected functions, lambda functions, loops, arbitrary blocks of code, etc. For optimization, this can be omitted for code that does not have preconditions, postconditions, and exception guarantees.

BOOST_CONTRACT_FUNCTION() expands to code equivalent to the following (note that no code is generated when BOOST_CONTRACT_NO_FUNCTIONS is defined):

#ifndef BOOST_CONTRACT_NO_FUNCTIONS
    boost::contract::check internal_var =
            boost::contract::function()
#endif

Where:

  • 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, Non-Member Functions, Private and Protected Functions, Lambdas, Loops, Code Blocks


PrevUpHomeNext