mpl_cheatsheet.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/config.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/ext/boost/mpl/vector.hpp>
  8. #include <boost/hana/ext/std/integral_constant.hpp>
  9. #include <boost/hana/integral_constant.hpp>
  10. #include <boost/hana/plus.hpp>
  11. #include <boost/hana/tuple.hpp>
  12. #include <boost/mpl/fold.hpp>
  13. #include <boost/mpl/if.hpp>
  14. #include <boost/mpl/int.hpp>
  15. #include <boost/mpl/next.hpp>
  16. #include <boost/mpl/placeholders.hpp>
  17. #include <boost/mpl/vector.hpp>
  18. #include <type_traits>
  19. namespace hana = boost::hana;
  20. namespace mpl = boost::mpl;
  21. namespace with_mpl {
  22. //! [mpl]
  23. using types = mpl::vector<long, float, short, float, long, long double>;
  24. using number_of_floats = mpl::fold<
  25. types,
  26. mpl::int_<0>,
  27. mpl::if_<std::is_floating_point<mpl::_2>,
  28. mpl::next<mpl::_1>,
  29. mpl::_1
  30. >
  31. >::type;
  32. static_assert(number_of_floats::value == 3, "");
  33. //! [mpl]
  34. }
  35. namespace with_hana {
  36. //! [hana]
  37. constexpr auto types = hana::tuple_t<long, float, short, float, long, long double>;
  38. BOOST_HANA_CONSTEXPR_LAMBDA auto number_of_floats = hana::fold_left(
  39. types,
  40. hana::int_c<0>,
  41. [](auto count, auto t) {
  42. return hana::if_(hana::trait<std::is_floating_point>(t),
  43. count + hana::int_c<1>,
  44. count
  45. );
  46. }
  47. );
  48. BOOST_HANA_CONSTANT_CHECK(number_of_floats == hana::int_c<3>);
  49. //! [hana]
  50. }
  51. int main() { }