containers.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <functional>
  6. #include <string>
  7. #include <type_traits>
  8. #include <utility>
  9. #include <vector>
  10. namespace hana = boost::hana;
  11. using namespace hana::literals;
  12. using namespace std::literals;
  13. int main() {
  14. {
  15. //! [make<tuple_tag>]
  16. auto xs = hana::make<hana::tuple_tag>(1, 2.2, 'a', "bcde"s);
  17. //! [make<tuple_tag>]
  18. }{
  19. //! [make<range_tag>]
  20. constexpr auto r = hana::make<hana::range_tag>(hana::int_c<3>, hana::int_c<10>);
  21. static_assert(r == hana::make_range(hana::int_c<3>, hana::int_c<10>), "");
  22. //! [make<range_tag>]
  23. }{
  24. //! [tuple_constructor]
  25. hana::tuple<int, double, char, std::string> xs{1, 2.2, 'a', "bcde"s};
  26. //! [tuple_constructor]
  27. (void)xs;
  28. }{
  29. //! [types]
  30. auto xs = hana::make_tuple(1, '2', "345");
  31. auto ints = hana::make_range(hana::int_c<0>, hana::int_c<100>);
  32. // what can we say about the types of `xs` and `ints`?
  33. //! [types]
  34. (void)xs;
  35. (void)ints;
  36. }{
  37. //! [types_maximally_specified]
  38. hana::tuple<int, char, char const*> xs = hana::make_tuple(1, '2', "345");
  39. auto ints = hana::make_range(hana::int_c<0>, hana::int_c<100>);
  40. // can't specify the type of ints, however
  41. //! [types_maximally_specified]
  42. (void)xs;
  43. (void)ints;
  44. }{
  45. //! [lifetime]
  46. std::string hello = "Hello";
  47. std::vector<char> world = {'W', 'o', 'r', 'l', 'd'};
  48. // hello is copied, world is moved-in
  49. auto xs = hana::make_tuple(hello, std::move(world));
  50. // s is a reference to the copy of hello inside xs.
  51. // It becomes a dangling reference as soon as xs is destroyed.
  52. std::string& s = xs[0_c];
  53. //! [lifetime]
  54. (void)s;
  55. }{
  56. //! [reference_wrapper]
  57. std::vector<int> ints = { /* huge vector of ints */ };
  58. std::vector<std::string> strings = { /* huge vector of strings */ };
  59. auto map = hana::make_map(
  60. hana::make_pair(hana::type_c<int>, std::ref(ints)),
  61. hana::make_pair(hana::type_c<std::string>, std::ref(strings))
  62. );
  63. auto& v = map[hana::type_c<int>].get();
  64. BOOST_HANA_RUNTIME_CHECK(&v == &ints);
  65. //! [reference_wrapper]
  66. }
  67. }
  68. namespace overloading {
  69. //! [overloading]
  70. template <typename T>
  71. void f(std::vector<T> xs) {
  72. // ...
  73. }
  74. template <typename R, typename = std::enable_if_t<hana::is_a<hana::range_tag, R>()>>
  75. void f(R r) {
  76. // ...
  77. }
  78. //! [overloading]
  79. }