• 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 //[introduction
11 #include <boost/contract.hpp>
12 
inc(int & x)13 void inc(int& x) {
14     boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); // Old value.
15     boost::contract::check c = boost::contract::function()
16         .precondition([&] {
17             BOOST_CONTRACT_ASSERT(x < std::numeric_limits<int>::max()); // Line 17.
18         })
19         .postcondition([&] {
20             BOOST_CONTRACT_ASSERT(x == *old_x + 1); // Line 20.
21         })
22     ;
23 
24     ++x; // Function body.
25 }
26 //]
27 
main()28 int main() {
29     int x = 10;
30     inc(x);
31     assert(x == 11);
32     return 0;
33 }
34 
35