equal_to_cxx14.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 call_if equality check with C++14 generic lambdas.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/call_if.hpp>
  8. #include <boost/type_traits/has_equal_to.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <functional> // std::bind for generic lambdas.
  11. #include <sstream>
  12. #include <ios>
  13. boost::contract::test::detail::oteststream out;
  14. struct x {}; // Does not have operator==.
  15. int main() {
  16. std::ostringstream ok;
  17. ok << std::boolalpha;
  18. out << std::boolalpha;
  19. x x1, x2;
  20. out.str("");
  21. out <<
  22. boost::contract::call_if<boost::has_equal_to<int> >(
  23. std::bind([] (auto a, auto b) { return a == b; }, 123, 456)
  24. ).else_([] { return true; })
  25. << std::endl;
  26. ok.str(""); ok << false << std::endl;
  27. BOOST_TEST(out.eq(ok.str()));
  28. out.str("");
  29. out <<
  30. boost::contract::call_if<boost::has_equal_to<x> >(
  31. std::bind([] (auto a, auto b) { return a == b; }, x1, x2)
  32. ).else_([] { return true; })
  33. << std::endl;
  34. ok.str(""); ok << true << std::endl;
  35. BOOST_TEST(out.eq(ok.str()));
  36. out.str("");
  37. out << // Test explicit result, cannot deduce from lambda missing `-> bool`.
  38. boost::contract::call_if<boost::has_equal_to<x> >(
  39. std::bind([] (auto a, auto b) { return a == b; }, x1, x2)
  40. ).else_([] { return true; })
  41. << std::endl;
  42. ok.str(""); ok << true << std::endl;
  43. BOOST_TEST(out.eq(ok.str()));
  44. return boost::report_errors();
  45. }