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 //[lambda 19 int total = 0; 20 std::for_each(v.cbegin(), v.cend(), 21 // Contract for a lambda function. 22 [&total] (int const x) { 23 boost::contract::old_ptr<int> old_total = 24 BOOST_CONTRACT_OLDOF(total); 25 boost::contract::check c = boost::contract::function() 26 .precondition([&] { 27 BOOST_CONTRACT_ASSERT( 28 total < std::numeric_limits<int>::max() - x); 29 }) 30 .postcondition([&] { 31 BOOST_CONTRACT_ASSERT(total == *old_total + x); 32 }) 33 ; 34 35 total += x; // Lambda function body. 36 } 37 ); 38 //] 39 40 assert(total == 6); 41 return 0; 42 } 43 44