1 /*=============================================================================
2 Copyright (c) 2012 Paul Fultz II
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
8 #ifndef BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H
9 #define BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H
17 /// The `match` function adaptor combines several functions together and
18 /// resolves which one should be called by using C++ overload resolution. This
19 /// is different than the [`first_of`](/include/boost/hof/conditional) adaptor which resolves
20 /// them based on order.
25 /// template<class... Fs>
26 /// constexpr match_adaptor<Fs...> match(Fs...fs);
33 /// * [ConstInvocable](ConstInvocable)
34 /// * MoveConstructible
39 /// #include <boost/hof.hpp>
40 /// using namespace boost::hof;
44 /// int operator()(int) const
55 /// foo operator()(foo) const
61 /// typedef match_adaptor<int_class, foo_class> fun;
63 /// static_assert(std::is_same<int, decltype(fun()(1))>::value, "Failed match");
64 /// static_assert(std::is_same<foo, decltype(fun()(foo()))>::value, "Failed match");
71 /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
72 /// Proposal for C++ generic overload function
75 #include <boost/hof/reveal.hpp>
76 #include <boost/hof/detail/callable_base.hpp>
77 #include <boost/hof/detail/delegate.hpp>
78 #include <boost/hof/detail/move.hpp>
79 #include <boost/hof/detail/make.hpp>
80 #include <boost/hof/detail/static_const_var.hpp>
82 namespace boost { namespace hof {
84 template<class...Fs> struct match_adaptor;
86 template<class F, class...Fs>
87 struct match_adaptor<F, Fs...> : detail::callable_base<F>, match_adaptor<Fs...>
89 typedef match_adaptor<Fs...> base;
90 typedef match_adaptor fit_rewritable_tag;
93 : failure_for<detail::callable_base<F>, Fs...>
96 BOOST_HOF_INHERIT_DEFAULT(match_adaptor, detail::callable_base<F>, base);
98 template<class X, class... Xs, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>), BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, Xs...)>
99 constexpr match_adaptor(X&& f1, Xs&& ... fs)
100 : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1)), base(BOOST_HOF_FORWARD(Xs)(fs)...)
104 using base::operator();
108 struct match_adaptor<F> : detail::callable_base<F>
110 typedef detail::callable_base<F> base;
111 typedef match_adaptor fit_rewritable_tag;
114 BOOST_HOF_INHERIT_CONSTRUCTOR(match_adaptor, detail::callable_base<F>);
117 BOOST_HOF_DECLARE_STATIC_VAR(match, detail::make<match_adaptor>);
119 }} // namespace boost::hof