unpack.hpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*!
  2. @file
  3. Forward declares `boost::hana::unpack`.
  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_UNPACK_HPP
  9. #define BOOST_HANA_FWD_UNPACK_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Invoke a function with the elements of a Foldable as arguments.
  14. //! @ingroup group-Foldable
  15. //!
  16. //! Given a function and a foldable structure whose length can be known at
  17. //! compile-time, `unpack` invokes the function with the contents of that
  18. //! structure. In other words, `unpack(xs, f)` is equivalent to `f(x...)`,
  19. //! where `x...` are the elements of the structure. The length of the
  20. //! structure must be known at compile-time, because the version of `f`'s
  21. //! `operator()` that will be compiled depends on the number of arguments
  22. //! it is called with, which has to be known at compile-time.
  23. //!
  24. //! To create a function that accepts a foldable instead of variadic
  25. //! arguments, see `fuse` instead.
  26. //!
  27. //!
  28. //! @param xs
  29. //! The structure to expand into the function.
  30. //!
  31. //! @param f
  32. //! A function to be invoked as `f(x...)`, where `x...` are the elements
  33. //! of the structure as-if they had been linearized with `to<tuple_tag>`.
  34. //!
  35. //!
  36. //! Example
  37. //! -------
  38. //! @include example/unpack.cpp
  39. //!
  40. //!
  41. //! Rationale: `unpack`'s name and parameter order
  42. //! ----------------------------------------------
  43. //! It has been suggested a couple of times that `unpack` be called
  44. //! `apply` instead, and that the parameter order be reversed to match
  45. //! that of the [proposed std::apply function][1]. However, the name
  46. //! `apply` is already used to denote normal function application, an use
  47. //! which is consistent with the Boost MPL library and with the rest of
  48. //! the world, especially the functional programming community.
  49. //! Furthermore, the author of this library considers the proposed
  50. //! `std::apply` to have both an unfortunate name and an unfortunate
  51. //! parameter order. Indeed, taking the function as the first argument
  52. //! means that using `std::apply` with a lambda function looks like
  53. //! @code
  54. //! std::apply([](auto ...args) {
  55. //! use(args...);
  56. //! }, tuple);
  57. //! @endcode
  58. //!
  59. //! which is undeniably ugly because of the trailing `, tuple)` part
  60. //! on the last line. On the other hand, taking the function as a
  61. //! second argument allows one to write
  62. //! @code
  63. //! hana::unpack(tuple, [](auto ...args) {
  64. //! use(args...);
  65. //! });
  66. //! @endcode
  67. //!
  68. //! which looks much nicer. Because of these observations, the author
  69. //! of this library feels justified to use `unpack` instead of `apply`,
  70. //! and to use a sane parameter order.
  71. //!
  72. //! [1]: http://en.cppreference.com/w/cpp/experimental/apply
  73. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  74. constexpr auto unpack = [](auto&& xs, auto&& f) -> decltype(auto) {
  75. return tag-dispatched;
  76. };
  77. #else
  78. template <typename T, typename = void>
  79. struct unpack_impl : unpack_impl<T, when<true>> { };
  80. struct unpack_t {
  81. template <typename Xs, typename F>
  82. constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
  83. };
  84. constexpr unpack_t unpack{};
  85. #endif
  86. BOOST_HANA_NAMESPACE_END
  87. #endif // !BOOST_HANA_FWD_UNPACK_HPP