introduction.cpp 872 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. #include <limits>
  6. #include <cassert>
  7. //[introduction
  8. #include <boost/contract.hpp>
  9. void inc(int& x) {
  10. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); // Old value.
  11. boost::contract::check c = boost::contract::function()
  12. .precondition([&] {
  13. BOOST_CONTRACT_ASSERT(x < std::numeric_limits<int>::max()); // Line 17.
  14. })
  15. .postcondition([&] {
  16. BOOST_CONTRACT_ASSERT(x == *old_x + 1); // Line 20.
  17. })
  18. ;
  19. ++x; // Function body.
  20. }
  21. //]
  22. int main() {
  23. int x = 10;
  24. inc(x);
  25. assert(x == 11);
  26. return 0;
  27. }