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 18 //[loop 19 int total = 0; 20 // Contract for a for-loop (same for while- and all other loops). 21 for(std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) { 22 boost::contract::old_ptr<int> old_total = BOOST_CONTRACT_OLDOF(total); 23 boost::contract::check c = boost::contract::function() 24 .precondition([&] { 25 BOOST_CONTRACT_ASSERT( 26 total < std::numeric_limits<int>::max() - *i); 27 }) 28 .postcondition([&] { 29 BOOST_CONTRACT_ASSERT(total == *old_total + *i); 30 }) 31 ; 32 33 total += *i; // For-loop body. 34 } 35 //] 36 37 assert(total == 6); 38 return 0; 39 } 40 41