code_block.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 <boost/contract.hpp>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <limits>
  9. int main() {
  10. std::vector<int> v;
  11. v.push_back(1);
  12. v.push_back(2);
  13. v.push_back(3);
  14. int total = 10;
  15. //[code_block
  16. /* ... */
  17. // Contract for a code block.
  18. { // Code block entry (check preconditions).
  19. boost::contract::old_ptr<int> old_total = BOOST_CONTRACT_OLDOF(total);
  20. boost::contract::check c = boost::contract::function()
  21. .precondition([&] {
  22. BOOST_CONTRACT_ASSERT(v.size() == 3);
  23. })
  24. .postcondition([&] {
  25. BOOST_CONTRACT_ASSERT(total == *old_total + v[0] + v[1] + v[2]);
  26. })
  27. ;
  28. total += v[0] + v[1] + v[2]; // Code block body.
  29. } // Code block exit (check postconditions and exceptions guarantees).
  30. /* ... */
  31. //]
  32. assert(total == 16);
  33. return 0;
  34. }