github_31.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/any_of.hpp>
  5. #include <boost/hana/assert.hpp>
  6. #include <boost/hana/bool.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/find_if.hpp>
  9. #include <boost/hana/fwd/at.hpp>
  10. #include <boost/hana/fwd/drop_front.hpp>
  11. #include <boost/hana/fwd/is_empty.hpp>
  12. #include <boost/hana/integral_constant.hpp>
  13. #include <boost/hana/optional.hpp>
  14. namespace hana = boost::hana;
  15. // A simple infinite Iterable.
  16. template <int i>
  17. struct counter { };
  18. namespace boost { namespace hana {
  19. template <int i>
  20. struct at_impl<counter<i>> {
  21. template <typename N>
  22. static constexpr auto apply(counter<i>, N const&) {
  23. return hana::int_c<i + N::value>;
  24. }
  25. };
  26. template <int i>
  27. struct drop_front_impl<counter<i>> {
  28. template <typename N>
  29. static constexpr auto apply(counter<i>, N) { return counter<i + N::value>{}; }
  30. };
  31. template <int i>
  32. struct is_empty_impl<counter<i>> {
  33. static constexpr auto apply(counter<i>) { return hana::false_c; }
  34. };
  35. }}
  36. int main() {
  37. // find_if and any_of should short-circuit and stop even though the
  38. // Iterable is infinite.
  39. BOOST_HANA_CONSTANT_CHECK(hana::any_of(counter<1>{}, hana::equal.to(hana::int_c<4>)));
  40. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  41. hana::find_if(counter<1>{}, hana::equal.to(hana::int_c<4>)),
  42. hana::just(hana::int_c<4>)
  43. ));
  44. }