Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:52:55

0001 /*!
0002 @file
0003 Forward declares `boost::hana::unpack`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FWD_UNPACK_HPP
0011 #define BOOST_HANA_FWD_UNPACK_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/core/when.hpp>
0015 
0016 
0017 namespace boost { namespace hana {
0018     //! Invoke a function with the elements of a Foldable as arguments.
0019     //! @ingroup group-Foldable
0020     //!
0021     //! Given a function and a foldable structure whose length can be known at
0022     //! compile-time, `unpack` invokes the function with the contents of that
0023     //! structure. In other words, `unpack(xs, f)` is equivalent to `f(x...)`,
0024     //! where `x...` are the elements of the structure. The length of the
0025     //! structure must be known at compile-time, because the version of `f`'s
0026     //! `operator()` that will be compiled depends on the number of arguments
0027     //! it is called with, which has to be known at compile-time.
0028     //!
0029     //! To create a function that accepts a foldable instead of variadic
0030     //! arguments, see `fuse` instead.
0031     //!
0032     //!
0033     //! @param xs
0034     //! The structure to expand into the function.
0035     //!
0036     //! @param f
0037     //! A function to be invoked as `f(x...)`, where `x...` are the elements
0038     //! of the structure as-if they had been linearized with `to<tuple_tag>`.
0039     //!
0040     //!
0041     //! Example
0042     //! -------
0043     //! @include example/unpack.cpp
0044     //!
0045     //!
0046     //! Rationale: `unpack`'s name and parameter order
0047     //! ----------------------------------------------
0048     //! It has been suggested a couple of times that `unpack` be called
0049     //! `apply` instead, and that the parameter order be reversed to match
0050     //! that of the [proposed std::apply function][1]. However, the name
0051     //! `apply` is already used to denote normal function application, an use
0052     //! which is consistent with the Boost MPL library and with the rest of
0053     //! the world, especially the functional programming community.
0054     //! Furthermore, the author of this library considers the proposed
0055     //! `std::apply` to have both an unfortunate name and an unfortunate
0056     //! parameter order. Indeed, taking the function as the first argument
0057     //! means that using `std::apply` with a lambda function looks like
0058     //! @code
0059     //! std::apply([](auto ...args) {
0060     //!     use(args...);
0061     //! }, tuple);
0062     //! @endcode
0063     //!
0064     //! which is undeniably ugly because of the trailing `, tuple)` part
0065     //! on the last line. On the other hand, taking the function as a
0066     //! second argument allows one to write
0067     //! @code
0068     //! hana::unpack(tuple, [](auto ...args) {
0069     //!     use(args...);
0070     //! });
0071     //! @endcode
0072     //!
0073     //! which looks much nicer. Because of these observations, the author
0074     //! of this library feels justified to use `unpack` instead of `apply`,
0075     //! and to use a sane parameter order.
0076     //!
0077     //! [1]: http://en.cppreference.com/w/cpp/experimental/apply
0078 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0079     constexpr auto unpack = [](auto&& xs, auto&& f) -> decltype(auto) {
0080         return tag-dispatched;
0081     };
0082 #else
0083     template <typename T, typename = void>
0084     struct unpack_impl : unpack_impl<T, when<true>> { };
0085 
0086     struct unpack_t {
0087         template <typename Xs, typename F>
0088         constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
0089     };
0090 
0091     BOOST_HANA_INLINE_VARIABLE constexpr unpack_t unpack{};
0092 #endif
0093 }} // end namespace boost::hana
0094 
0095 #endif // !BOOST_HANA_FWD_UNPACK_HPP