tuple.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*!
  2. @file
  3. Forward declares `boost::hana::tuple`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_TUPLE_HPP
  9. #define BOOST_HANA_FWD_TUPLE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/fwd/core/make.hpp>
  12. #include <boost/hana/fwd/core/to.hpp>
  13. #include <boost/hana/fwd/integral_constant.hpp>
  14. #include <boost/hana/fwd/type.hpp>
  15. BOOST_HANA_NAMESPACE_BEGIN
  16. //! @ingroup group-datatypes
  17. //! General purpose index-based heterogeneous sequence with a fixed length.
  18. //!
  19. //! The tuple is the bread and butter for static metaprogramming.
  20. //! Conceptually, it is like a `std::tuple`; it is a container able
  21. //! of holding objects of different types and whose size is fixed at
  22. //! compile-time. However, Hana's tuple provides much more functionality
  23. //! than its `std` counterpart, and it is also much more efficient than
  24. //! all standard library implementations tested so far.
  25. //!
  26. //! Tuples are index-based sequences. If you need an associative
  27. //! sequence with a key-based access, then you should consider
  28. //! `hana::map` or `hana::set` instead.
  29. //!
  30. //! @note
  31. //! When you use a container, remember not to make assumptions about its
  32. //! representation, unless the documentation gives you those guarantees.
  33. //! More details [in the tutorial](@ref tutorial-containers-types).
  34. //!
  35. //!
  36. //! Modeled concepts
  37. //! ----------------
  38. //! `Sequence`, and all the concepts it refines
  39. //!
  40. //!
  41. //! Provided operators
  42. //! ------------------
  43. //! For convenience, the following operators are provided:
  44. //! @code
  45. //! xs == ys -> equal(xs, ys)
  46. //! xs != ys -> not_equal(xs, ys)
  47. //!
  48. //! xs < ys -> less(xs, ys)
  49. //! xs <= ys -> less_equal(xs, ys)
  50. //! xs > ys -> greater(xs, ys)
  51. //! xs >= ys -> greater_equal(xs, ys)
  52. //!
  53. //! xs | f -> chain(xs, f)
  54. //!
  55. //! xs[n] -> at(xs, n)
  56. //! @endcode
  57. //!
  58. //!
  59. //! Example
  60. //! -------
  61. //! @include example/tuple/tuple.cpp
  62. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  63. template <typename ...Xn>
  64. struct tuple {
  65. //! Default constructs the `tuple`. Only exists when all the elements
  66. //! of the tuple are default constructible.
  67. constexpr tuple();
  68. //! Initialize each element of the tuple with the corresponding element
  69. //! from `xn...`. Only exists when all the elements of the tuple are
  70. //! copy-constructible.
  71. //!
  72. //! @note
  73. //! Unlike the corresponding constructor for `std::tuple`, this
  74. //! constructor is not explicit. This allows returning a tuple
  75. //! from a function with the brace-initialization syntax.
  76. constexpr tuple(Xn const& ...xn);
  77. //! Initialize each element of the tuple by perfect-forwarding the
  78. //! corresponding element in `yn...`. Only exists when all the
  79. //! elements of the created tuple are constructible from the
  80. //! corresponding perfect-forwarded value.
  81. //!
  82. //! @note
  83. //! Unlike the corresponding constructor for `std::tuple`, this
  84. //! constructor is not explicit. This allows returning a tuple
  85. //! from a function with the brace-initialization syntax.
  86. template <typename ...Yn>
  87. constexpr tuple(Yn&& ...yn);
  88. //! Copy-initialize a tuple from another tuple. Only exists when all
  89. //! the elements of the constructed tuple are copy-constructible from
  90. //! the corresponding element in the source tuple.
  91. template <typename ...Yn>
  92. constexpr tuple(tuple<Yn...> const& other);
  93. //! Move-initialize a tuple from another tuple. Only exists when all
  94. //! the elements of the constructed tuple are move-constructible from
  95. //! the corresponding element in the source tuple.
  96. template <typename ...Yn>
  97. constexpr tuple(tuple<Yn...>&& other);
  98. //! Assign a tuple to another tuple. Only exists when all the elements
  99. //! of the destination tuple are assignable from the corresponding
  100. //! element in the source tuple.
  101. template <typename ...Yn>
  102. constexpr tuple& operator=(tuple<Yn...> const& other);
  103. //! Move-assign a tuple to another tuple. Only exists when all the
  104. //! elements of the destination tuple are move-assignable from the
  105. //! corresponding element in the source tuple.
  106. template <typename ...Yn>
  107. constexpr tuple& operator=(tuple<Yn...>&& other);
  108. //! Equivalent to `hana::chain`.
  109. template <typename ...T, typename F>
  110. friend constexpr auto operator|(tuple<T...>, F);
  111. //! Equivalent to `hana::equal`
  112. template <typename X, typename Y>
  113. friend constexpr auto operator==(X&& x, Y&& y);
  114. //! Equivalent to `hana::not_equal`
  115. template <typename X, typename Y>
  116. friend constexpr auto operator!=(X&& x, Y&& y);
  117. //! Equivalent to `hana::less`
  118. template <typename X, typename Y>
  119. friend constexpr auto operator<(X&& x, Y&& y);
  120. //! Equivalent to `hana::greater`
  121. template <typename X, typename Y>
  122. friend constexpr auto operator>(X&& x, Y&& y);
  123. //! Equivalent to `hana::less_equal`
  124. template <typename X, typename Y>
  125. friend constexpr auto operator<=(X&& x, Y&& y);
  126. //! Equivalent to `hana::greater_equal`
  127. template <typename X, typename Y>
  128. friend constexpr auto operator>=(X&& x, Y&& y);
  129. //! Equivalent to `hana::at`
  130. template <typename N>
  131. constexpr decltype(auto) operator[](N&& n);
  132. };
  133. #else
  134. template <typename ...Xn>
  135. struct tuple;
  136. #endif
  137. //! Tag representing `hana::tuple`s.
  138. //! @related tuple
  139. struct tuple_tag { };
  140. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  141. //! Function object for creating a `tuple`.
  142. //! @relates hana::tuple
  143. //!
  144. //! Given zero or more objects `xs...`, `make<tuple_tag>` returns a new tuple
  145. //! containing those objects. The elements are held by value inside the
  146. //! resulting tuple, and they are hence copied or moved in. This is
  147. //! analogous to `std::make_tuple` for creating Hana tuples.
  148. //!
  149. //!
  150. //! Example
  151. //! -------
  152. //! @include example/tuple/make.cpp
  153. template <>
  154. constexpr auto make<tuple_tag> = [](auto&& ...xs) {
  155. return tuple<std::decay_t<decltype(xs)>...>{forwarded(xs)...};
  156. };
  157. #endif
  158. //! Alias to `make<tuple_tag>`; provided for convenience.
  159. //! @relates hana::tuple
  160. constexpr auto make_tuple = make<tuple_tag>;
  161. //! Equivalent to `to<tuple_tag>`; provided for convenience.
  162. //! @relates hana::tuple
  163. constexpr auto to_tuple = to<tuple_tag>;
  164. //! Create a tuple specialized for holding `hana::type`s.
  165. //! @relates hana::tuple
  166. //!
  167. //! This is functionally equivalent to `make<tuple_tag>(type_c<T>...)`, except
  168. //! that using `tuple_t` allows the library to perform some compile-time
  169. //! optimizations. Also note that the type of the objects returned by
  170. //! `tuple_t` and an equivalent call to `make<tuple_tag>` may differ.
  171. //!
  172. //!
  173. //! Example
  174. //! -------
  175. //! @include example/tuple/tuple_t.cpp
  176. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  177. template <typename ...T>
  178. constexpr implementation_defined tuple_t{};
  179. #else
  180. template <typename ...T>
  181. constexpr hana::tuple<hana::type<T>...> tuple_t{};
  182. #endif
  183. //! Create a tuple specialized for holding `hana::integral_constant`s.
  184. //! @relates hana::tuple
  185. //!
  186. //! This is functionally equivalent to `make<tuple_tag>(integral_c<T, v>...)`,
  187. //! except that using `tuple_c` allows the library to perform some
  188. //! compile-time optimizations. Also note that the type of the objects
  189. //! returned by `tuple_c` and an equivalent call to `make<tuple_tag>` may differ.
  190. //!
  191. //!
  192. //! Example
  193. //! -------
  194. //! @include example/tuple/tuple_c.cpp
  195. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  196. template <typename T, T ...v>
  197. constexpr implementation_defined tuple_c{};
  198. #else
  199. template <typename T, T ...v>
  200. constexpr hana::tuple<hana::integral_constant<T, v>...> tuple_c{};
  201. #endif
  202. BOOST_HANA_NAMESPACE_END
  203. #endif // !BOOST_HANA_FWD_TUPLE_HPP