indirect.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. indirect.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/hof/indirect.hpp>
  8. #include "test.hpp"
  9. BOOST_HOF_TEST_CASE()
  10. {
  11. BOOST_HOF_TEST_CHECK(3 == boost::hof::indirect(std::unique_ptr<binary_class>(new binary_class()))(1, 2));
  12. BOOST_HOF_TEST_CHECK(3 == boost::hof::reveal(boost::hof::indirect(std::unique_ptr<binary_class>(new binary_class())))(1, 2));
  13. binary_class f;
  14. BOOST_HOF_TEST_CHECK(3 == boost::hof::indirect(&f)(1, 2));
  15. BOOST_HOF_TEST_CHECK(3 == boost::hof::reveal(boost::hof::indirect(&f))(1, 2));
  16. }
  17. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  18. BOOST_HOF_TEST_CASE()
  19. {
  20. binary_class f;
  21. static_assert(noexcept(boost::hof::indirect(&f)(1, 2)), "noexcept indirect");
  22. }
  23. #endif
  24. struct mutable_function
  25. {
  26. mutable_function() : value(0) {}
  27. void operator()(int a) { value += a; }
  28. int value;
  29. };
  30. BOOST_HOF_TEST_CASE()
  31. {
  32. auto mf = mutable_function{};
  33. boost::hof::indirect(&mf)(15);
  34. boost::hof::indirect(&mf)(2);
  35. BOOST_HOF_TEST_CHECK(mf.value == 17);
  36. }
  37. BOOST_HOF_TEST_CASE()
  38. {
  39. auto mf = std::make_shared<mutable_function>();
  40. boost::hof::indirect(mf)(15);
  41. boost::hof::indirect(mf)(2);
  42. BOOST_HOF_TEST_CHECK(mf->value == 17);
  43. }