C++ boost::contract::function()














































C++ boost::contract::function()



Description:-
Contract programming allows to specify preconditions, postconditions, and class invariants that are automatically checked when functions are executed at run-time.

Syntax:-
 boost::contract::function();

Headers:-
#include <boost/contract.hpp> #include <cassert>

Code:-
#include<iostream> #include <boost/contract.hpp>//header for contract function #include <cassert> using namespace std; int factorial(int n) { int result; boost::contract::check c = boost::contract::function() .precondition([&] { BOOST_CONTRACT_ASSERT(n >= 0); // Non-negative natural number. BOOST_CONTRACT_ASSERT(n <= 12); // Max function input. }) .postcondition([&] { BOOST_CONTRACT_ASSERT(result >= 1); if(n < 2) { BOOST_CONTRACT_ASSERT(result == 1); } else { BOOST_CONTRACT_ASSERT_AUDIT(n * factorial(n - 1)); } }) ; return n < 2 ? (result = 1) : (result = n * factorial(n - 1)); } int main() { cout<<"Factorial="<<factorial(5);//Printing factorial return 0; }

Output:-
Factorial=120









Comments