lexicographical_compare.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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/integral_constant.hpp>
  6. #include <boost/hana/less.hpp>
  7. #include <boost/hana/lexicographical_compare.hpp>
  8. #include <boost/hana/tuple.hpp>
  9. #include <boost/hana/type.hpp>
  10. namespace hana = boost::hana;
  11. int main() {
  12. // works with elements whose `less` does not return a Constant
  13. {
  14. constexpr auto xs = hana::make_tuple(1, 2, 3, 4);
  15. constexpr auto ys = hana::make_tuple(1, 6, 3, 4);
  16. static_assert(hana::lexicographical_compare(xs, ys), "");
  17. }
  18. // and with those that do
  19. {
  20. auto xs = hana::make_tuple(hana::int_c<1>, hana::int_c<2>, hana::int_c<3>);
  21. auto ys = hana::make_tuple(hana::int_c<1>, hana::int_c<5>, hana::int_c<3>);
  22. BOOST_HANA_CONSTANT_CHECK(hana::lexicographical_compare(xs, ys));
  23. }
  24. // it also accepts a custom predicate
  25. {
  26. auto xs = hana::make_tuple(hana::type_c<int>, hana::type_c<char>, hana::type_c<void*>);
  27. auto ys = hana::make_tuple(hana::type_c<int>, hana::type_c<long>, hana::type_c<void*>);
  28. BOOST_HANA_CONSTANT_CHECK(
  29. hana::lexicographical_compare(xs, ys, [](auto t, auto u) {
  30. return hana::sizeof_(t) < hana::sizeof_(u);
  31. })
  32. );
  33. }
  34. }