• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits>
8 #include <cassert>
9 
10 //[non_member
11 #include <boost/contract.hpp>
12 
13 // Contract for a non-member function.
inc(int & x)14 int inc(int& x) {
15     int result;
16     boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
17     boost::contract::check c = boost::contract::function()
18         .precondition([&] {
19             BOOST_CONTRACT_ASSERT(x < std::numeric_limits<int>::max());
20         })
21         .postcondition([&] {
22             BOOST_CONTRACT_ASSERT(x == *old_x + 1);
23             BOOST_CONTRACT_ASSERT(result == *old_x);
24         })
25         .except([&] {
26             BOOST_CONTRACT_ASSERT(x == *old_x);
27         })
28     ;
29 
30     return result = x++; // Function body.
31 }
32 //]
33 
main()34 int main() {
35     int x = std::numeric_limits<int>::max() - 1;
36     assert(inc(x) == std::numeric_limits<int>::max() - 1);
37     assert(x == std::numeric_limits<int>::max());
38     return 0;
39 }
40 
41