1 2 // Copyright (C) 2008-2018 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 (see accompanying 4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 6 7 #include <boost/contract.hpp> 8 #include <vector> 9 #include <algorithm> 10 #include <limits> 11 main()12int main() { 13 std::vector<int> v; 14 v.push_back(1); 15 v.push_back(2); 16 v.push_back(3); 17 int total = 10; 18 19 //[code_block 20 /* ... */ 21 22 // Contract for a code block. 23 { // Code block entry (check preconditions). 24 boost::contract::old_ptr<int> old_total = BOOST_CONTRACT_OLDOF(total); 25 boost::contract::check c = boost::contract::function() 26 .precondition([&] { 27 BOOST_CONTRACT_ASSERT(v.size() == 3); 28 }) 29 .postcondition([&] { 30 BOOST_CONTRACT_ASSERT(total == *old_total + v[0] + v[1] + v[2]); 31 }) 32 ; 33 34 total += v[0] + v[1] + v[2]; // Code block body. 35 } // Code block exit (check postconditions and exceptions guarantees). 36 37 /* ... */ 38 //] 39 40 assert(total == 16); 41 return 0; 42 } 43 44