3 Defines `boost::hana::apply`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_FUNCTIONAL_APPLY_HPP
11 #define BOOST_HANA_FUNCTIONAL_APPLY_HPP
13 #include <boost/hana/config.hpp>
16 BOOST_HANA_NAMESPACE_BEGIN
17 //! @ingroup group-functional
18 //! Invokes a Callable with the given arguments.
20 //! This is equivalent to [std::invoke][1] that will be added in C++17.
21 //! However, `apply` is a function object instead of a function, which
22 //! makes it possible to pass it to higher-order algorithms.
26 //! A [Callable][2] to be invoked with the given arguments.
29 //! The arguments to call `f` with. The number of `x...` must match the
35 //! @include example/functional/apply.cpp
37 //! [1]: http://en.cppreference.com/w/cpp/utility/functional/invoke
38 //! [2]: http://en.cppreference.com/w/cpp/concept/Callable
39 #ifdef BOOST_HANA_DOXYGEN_INVOKED
40 constexpr auto apply = [](auto&& f, auto&& ...x) -> decltype(auto) {
41 return forwarded(f)(forwarded(x)...);
45 template <typename F, typename... Args>
46 constexpr auto operator()(F&& f, Args&&... args) const ->
47 decltype(static_cast<F&&>(f)(static_cast<Args&&>(args)...))
49 return static_cast<F&&>(f)(static_cast<Args&&>(args)...);
52 template <typename Base, typename T, typename Derived>
53 constexpr auto operator()(T Base::*pmd, Derived&& ref) const ->
54 decltype(static_cast<Derived&&>(ref).*pmd)
56 return static_cast<Derived&&>(ref).*pmd;
59 template <typename PMD, typename Pointer>
60 constexpr auto operator()(PMD pmd, Pointer&& ptr) const ->
61 decltype((*static_cast<Pointer&&>(ptr)).*pmd)
63 return (*static_cast<Pointer&&>(ptr)).*pmd;
66 template <typename Base, typename T, typename Derived, typename... Args>
67 constexpr auto operator()(T Base::*pmf, Derived&& ref, Args&&... args) const ->
68 decltype((static_cast<Derived&&>(ref).*pmf)(static_cast<Args&&>(args)...))
70 return (static_cast<Derived&&>(ref).*pmf)(static_cast<Args&&>(args)...);
73 template <typename PMF, typename Pointer, typename... Args>
74 constexpr auto operator()(PMF pmf, Pointer&& ptr, Args&& ...args) const ->
75 decltype(((*static_cast<Pointer&&>(ptr)).*pmf)(static_cast<Args&&>(args)...))
77 return ((*static_cast<Pointer&&>(ptr)).*pmf)(static_cast<Args&&>(args)...);
81 constexpr apply_t apply{};
83 BOOST_HANA_NAMESPACE_END
85 #endif // !BOOST_HANA_FUNCTIONAL_APPLY_HPP