slice.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/assert.hpp>
  5. #include <boost/hana/core/to.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/filter.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include <boost/hana/length.hpp>
  10. #include <boost/hana/mod.hpp>
  11. #include <boost/hana/range.hpp>
  12. #include <boost/hana/slice.hpp>
  13. #include <boost/hana/tuple.hpp>
  14. #include <boost/hana/type.hpp>
  15. namespace hana = boost::hana;
  16. using namespace hana::literals;
  17. // Slice a contiguous range
  18. constexpr auto xs = hana::make_tuple(0, '1', 2.2, 3_c, hana::type_c<float>);
  19. static_assert(
  20. hana::slice(xs, hana::tuple_c<std::size_t, 1, 2, 3>) ==
  21. hana::make_tuple('1', 2.2, 3_c)
  22. , "");
  23. // A more complex example with a non-contiguous range
  24. constexpr auto letters = hana::to_tuple(hana::range_c<char, 'a', 'z'>);
  25. constexpr auto indices = hana::to_tuple(hana::make_range(hana::size_c<0>, hana::length(letters)));
  26. auto even_indices = hana::filter(indices, [](auto n) {
  27. return n % hana::size_c<2> == hana::size_c<0>;
  28. });
  29. BOOST_HANA_CONSTANT_CHECK(
  30. hana::slice(letters, even_indices) == hana::tuple_c<char,
  31. 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y'
  32. >
  33. );
  34. int main() { }