if_copyable_error.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. // Test forcing compiler error for old values of non-copyable types.
  6. #include "if_copyable.hpp"
  7. #include <boost/contract/function.hpp>
  8. #include <boost/contract/old.hpp>
  9. #include <boost/contract/check.hpp>
  10. #include <boost/contract/assert.hpp>
  11. #include <boost/noncopyable.hpp>
  12. template<typename T>
  13. void next(T& x) {
  14. boost::contract::old_ptr<T> old_x = BOOST_CONTRACT_OLDOF(x);
  15. boost::contract::check c = boost::contract::function()
  16. .postcondition([&] {
  17. // No need to check `if(old_x) ...` here.
  18. BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
  19. #ifdef BOOST_CONTRACT_NO_ALL
  20. #error "force error if no contracts (ASSERT expands to nothing)"
  21. #endif
  22. })
  23. ;
  24. ++x;
  25. }
  26. int main() {
  27. int i = 1; // Test built-in copyable type.
  28. cp c(1); // Test custom copyable type.
  29. ncp n(1); // Test non-copyable type.
  30. next(i); // OK.
  31. next(c); // OK.
  32. next(n); // Error.
  33. return 0;
  34. }