equal.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. //[n1962_equal
  6. #include <boost/contract.hpp>
  7. #include <cassert>
  8. // Forward declaration because == and != contracts use one another's function.
  9. template<typename T>
  10. bool operator==(T const& left, T const& right);
  11. template<typename T>
  12. bool operator!=(T const& left, T const& right) {
  13. bool result;
  14. boost::contract::check c = boost::contract::function()
  15. .postcondition([&] {
  16. BOOST_CONTRACT_ASSERT(result == !(left == right));
  17. })
  18. ;
  19. return result = (left.value != right.value);
  20. }
  21. template<typename T>
  22. bool operator==(T const& left, T const& right) {
  23. bool result;
  24. boost::contract::check c = boost::contract::function()
  25. .postcondition([&] {
  26. BOOST_CONTRACT_ASSERT(result == !(left != right));
  27. })
  28. ;
  29. return result = (left.value == right.value);
  30. }
  31. struct number { int value; };
  32. int main() {
  33. number n;
  34. n.value = 123;
  35. assert((n == n) == true); // Explicitly call operator==.
  36. assert((n != n) == false); // Explicitly call operator!=.
  37. return 0;
  38. }
  39. //]