algorithms.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.hpp>
  5. #include <boost/hana/ext/std/integral_constant.hpp>
  6. #include <sstream>
  7. #include <string>
  8. #include <tuple>
  9. #include <type_traits>
  10. namespace hana = boost::hana;
  11. using namespace hana::literals;
  12. using namespace std::literals;
  13. int main() {
  14. {
  15. //! [reverse_transform]
  16. auto to_str = [](auto const& x) {
  17. std::stringstream ss;
  18. ss << x;
  19. return ss.str();
  20. };
  21. auto xs = hana::make_tuple(1, 2.2, 'a', "bcde");
  22. BOOST_HANA_RUNTIME_CHECK(
  23. hana::reverse(hana::transform(xs, to_str)) == hana::make_tuple("bcde", "a", "2.2", "1")
  24. );
  25. //! [reverse_transform]
  26. //! [reverse_transform_copy]
  27. hana::reverse(
  28. hana::transform(xs, to_str) // <-- copy into reverse(...) here?
  29. );
  30. //! [reverse_transform_copy]
  31. //! [reverse_transform_move]
  32. hana::reverse(
  33. hana::transform(xs, to_str) // <-- nope, move from the temporary instead!
  34. );
  35. //! [reverse_transform_move]
  36. }{
  37. //! [effects]
  38. auto r = hana::any_of(hana::make_tuple("hello"s, 1.2, 3), [](auto x) {
  39. return std::is_integral<decltype(x)>{};
  40. });
  41. BOOST_HANA_CONSTANT_CHECK(r);
  42. //! [effects]
  43. {
  44. //! [effects.codegen]
  45. auto xs = hana::make_tuple("hello"s, 1.2, 3);
  46. auto pred = [](auto x) { return std::is_integral<decltype(x)>{}; };
  47. auto r = hana::bool_c<
  48. decltype(pred(xs[0_c]))::value ? true :
  49. decltype(pred(xs[1_c]))::value ? true :
  50. decltype(pred(xs[2_c]))::value ? true :
  51. false
  52. >;
  53. BOOST_HANA_CONSTANT_CHECK(r);
  54. //! [effects.codegen]
  55. }
  56. }{
  57. //! [cross_phase.setup]
  58. struct Fish { std::string name; };
  59. struct Cat { std::string name; };
  60. struct Dog { std::string name; };
  61. auto animals = hana::make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
  62. // ^^^^^^^ not a compile-time value
  63. BOOST_HANA_CONSTANT_CHECK(hana::length(animals) == hana::size_c<3>);
  64. // ^^^^^^^^^^^^^^^^^^^^^ assertion done at compile-time
  65. //! [cross_phase.setup]
  66. //! [cross_phase.is_empty]
  67. BOOST_HANA_CONSTANT_CHECK(!hana::is_empty(animals));
  68. // ^^^^^^^^^^^^^^^^^^^^^^^ assertion done at compile-time
  69. //! [cross_phase.is_empty]
  70. {
  71. //! [cross_phase.any_of_runtime]
  72. bool any_garfield = hana::any_of(animals, [](auto animal) {
  73. return animal.name == "Garfield"s;
  74. });
  75. BOOST_HANA_RUNTIME_CHECK(any_garfield);
  76. //! [cross_phase.any_of_runtime]
  77. }{
  78. //! [cross_phase.any_of_compile_time]
  79. auto any_cat = hana::any_of(animals, [](auto x) {
  80. return std::is_same<decltype(x), Cat>{};
  81. });
  82. BOOST_HANA_CONSTANT_CHECK(any_cat);
  83. //! [cross_phase.any_of_compile_time]
  84. }{
  85. //! [cross_phase.any_of_explicit]
  86. hana::integral_constant<bool, true> any_cat = hana::any_of(animals, [](auto x) {
  87. return std::is_same<decltype(x), Cat>{};
  88. });
  89. BOOST_HANA_CONSTANT_CHECK(any_cat);
  90. //! [cross_phase.any_of_explicit]
  91. }{
  92. //! [cross_phase.filter]
  93. auto mammals = hana::filter(animals, [](auto animal) {
  94. return hana::type_c<decltype(animal)> != hana::type_c<Fish>;
  95. });
  96. //! [cross_phase.filter]
  97. BOOST_HANA_RUNTIME_CHECK(
  98. hana::transform(mammals, [](auto x) { return x.name; })
  99. == hana::make_tuple("Garfield", "Snoopy")
  100. );
  101. }
  102. }{
  103. //! [cross_phase.std::tuple_size]
  104. std::tuple<int, char, std::string> xs{1, '2', std::string{"345"}};
  105. static_assert(std::tuple_size<decltype(xs)>::value == 3u, "");
  106. //! [cross_phase.std::tuple_size]
  107. }
  108. }