]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::apply`. | |
4 | ||
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) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_FUNCTIONAL_APPLY_HPP | |
11 | #define BOOST_HANA_FUNCTIONAL_APPLY_HPP | |
12 | ||
13 | #include <boost/hana/config.hpp> | |
14 | ||
15 | ||
16 | BOOST_HANA_NAMESPACE_BEGIN | |
17 | //! @ingroup group-functional | |
18 | //! Invokes a Callable with the given arguments. | |
19 | //! | |
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. | |
23 | //! | |
24 | //! | |
25 | //! @param f | |
26 | //! A [Callable][2] to be invoked with the given arguments. | |
27 | //! | |
28 | //! @param x... | |
29 | //! The arguments to call `f` with. The number of `x...` must match the | |
30 | //! arity of `f`. | |
31 | //! | |
32 | //! | |
33 | //! Example | |
34 | //! ------- | |
35 | //! @include example/functional/apply.cpp | |
36 | //! | |
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)...); | |
42 | }; | |
43 | #else | |
44 | struct apply_t { | |
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)...)) | |
48 | { | |
49 | return static_cast<F&&>(f)(static_cast<Args&&>(args)...); | |
50 | } | |
51 | ||
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) | |
55 | { | |
56 | return static_cast<Derived&&>(ref).*pmd; | |
57 | } | |
58 | ||
59 | template <typename PMD, typename Pointer> | |
60 | constexpr auto operator()(PMD pmd, Pointer&& ptr) const -> | |
61 | decltype((*static_cast<Pointer&&>(ptr)).*pmd) | |
62 | { | |
63 | return (*static_cast<Pointer&&>(ptr)).*pmd; | |
64 | } | |
65 | ||
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)...)) | |
69 | { | |
70 | return (static_cast<Derived&&>(ref).*pmf)(static_cast<Args&&>(args)...); | |
71 | } | |
72 | ||
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)...)) | |
76 | { | |
77 | return ((*static_cast<Pointer&&>(ptr)).*pmf)(static_cast<Args&&>(args)...); | |
78 | } | |
79 | }; | |
80 | ||
81 | constexpr apply_t apply{}; | |
82 | #endif | |
83 | BOOST_HANA_NAMESPACE_END | |
84 | ||
85 | #endif // !BOOST_HANA_FUNCTIONAL_APPLY_HPP |