3 Defines `boost::hana::overload_linearly`.
5 @copyright Louis Dionne 2013-2017
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_OVERLOAD_LINEARLY_HPP
11 #define BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/detail/decay.hpp>
19 BOOST_HANA_NAMESPACE_BEGIN
20 //! @ingroup group-functional
21 //! Call the first function that produces a valid call expression.
23 //! Given functions `f1, ..., fn`, `overload_linearly(f1, ..., fn)` is
24 //! a new function that calls the first `fk` producing a valid call
25 //! expression with the given arguments. Specifically,
27 //! overload_linearly(f1, ..., fn)(args...) == fk(args...)
30 //! where `fk` is the _first_ function such that `fk(args...)` is a valid
36 //! @include example/functional/overload_linearly.cpp
37 #ifdef BOOST_HANA_DOXYGEN_INVOKED
38 constexpr auto overload_linearly = [](auto&& f1, auto&& f2, ..., auto&& fn) {
39 return [perfect-capture](auto&& ...x) -> decltype(auto) {
40 return forwarded(fk)(forwarded(x)...);
44 template <typename F, typename G>
45 struct overload_linearly_t {
50 template <typename ...Args, typename =
51 decltype(std::declval<F const&>()(std::declval<Args>()...))>
52 constexpr F const& which(int) const& { return f; }
54 template <typename ...Args, typename =
55 decltype(std::declval<F&>()(std::declval<Args>()...))>
56 constexpr F& which(int) & { return f; }
58 template <typename ...Args, typename =
59 decltype(std::declval<F&&>()(std::declval<Args>()...))>
60 constexpr F which(int) && { return static_cast<F&&>(f); }
62 template <typename ...Args>
63 constexpr G const& which(long) const& { return g; }
65 template <typename ...Args>
66 constexpr G& which(long) & { return g; }
68 template <typename ...Args>
69 constexpr G which(long) && { return static_cast<G&&>(g); }
72 template <typename ...Args>
73 constexpr decltype(auto) operator()(Args&& ...args) const&
74 { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
76 template <typename ...Args>
77 constexpr decltype(auto) operator()(Args&& ...args) &
78 { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
80 template <typename ...Args>
81 constexpr decltype(auto) operator()(Args&& ...args) &&
82 { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
85 struct make_overload_linearly_t {
86 template <typename F, typename G>
87 constexpr overload_linearly_t<
88 typename detail::decay<F>::type,
89 typename detail::decay<G>::type
90 > operator()(F&& f, G&& g) const {
91 return {static_cast<F&&>(f), static_cast<G&&>(g)};
94 template <typename F, typename G, typename ...H>
95 constexpr decltype(auto) operator()(F&& f, G&& g, H&& ...h) const {
96 return (*this)(static_cast<F&&>(f),
97 (*this)(static_cast<G&&>(g), static_cast<H&&>(h)...));
100 template <typename F>
101 constexpr typename detail::decay<F>::type operator()(F&& f) const {
102 return static_cast<F&&>(f);
106 constexpr make_overload_linearly_t overload_linearly{};
108 BOOST_HANA_NAMESPACE_END
110 #endif // !BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP