no_equal_error.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 assertion error when operations to check them missing (e.g., `==`).
  6. #include <boost/contract/function.hpp>
  7. #include <boost/contract/check.hpp>
  8. #include <boost/contract/assert.hpp>
  9. #include <vector>
  10. template<typename T>
  11. void push_back(std::vector<T>& vect, T const& value) {
  12. boost::contract::check c = boost::contract::function()
  13. .postcondition([&] {
  14. BOOST_CONTRACT_ASSERT(vect.back() == value); // Error (j has no ==).
  15. #ifdef BOOST_CONTRACT_NO_ALL
  16. #error "force error if no contracts (ASSERT expands to nothing)"
  17. #endif
  18. })
  19. ;
  20. vect.push_back(value);
  21. }
  22. struct j { // Type without operator==.
  23. explicit j(int /* i */) {}
  24. };
  25. int main() {
  26. std::vector<int> vi;
  27. push_back(vi, 123);
  28. j jj(456);
  29. std::vector<j> vj;
  30. push_back(vj, jj);
  31. return 0;
  32. }