non_member.cpp 1.1 KB

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