/*! @file Defines `boost::hana::overload`. @copyright Louis Dionne 2013-2017 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_FUNCTIONAL_OVERLOAD_HPP #define BOOST_HANA_FUNCTIONAL_OVERLOAD_HPP #include #include namespace boost { namespace hana { //! @ingroup group-functional //! Pick one of several functions to call based on overload resolution. //! //! Specifically, `overload(f1, f2, ..., fn)` is a function object such //! that //! @code //! overload(f1, f2, ..., fn)(x...) == fk(x...) //! @endcode //! //! where `fk` is the function of `f1, ..., fn` that would be called if //! overload resolution was performed amongst that set of functions only. //! If more than one function `fk` would be picked by overload resolution, //! then the call is ambiguous. //! //! ### Example //! @include example/functional/overload.cpp #ifdef BOOST_HANA_DOXYGEN_INVOKED constexpr auto overload = [](auto&& f1, auto&& f2, ..., auto&& fn) { return [perfect-capture](auto&& ...x) -> decltype(auto) { return forwarded(fk)(forwarded(x)...); }; }; #else template struct overload_t : overload_t::type , overload_t::type { using type = overload_t; using overload_t::type::operator(); using overload_t::type::operator(); template constexpr explicit overload_t(F_&& f, G_&& ...g) : overload_t::type(static_cast(f)) , overload_t::type(static_cast(g)...) { } }; template struct overload_t { using type = F; }; template struct overload_t { using type = overload_t; R (*fptr_)(Args...); explicit constexpr overload_t(R (*fp)(Args...)) : fptr_(fp) { } constexpr R operator()(Args ...args) const { return fptr_(static_cast(args)...); } }; struct make_overload_t { template ::type... >::type > constexpr Overload operator()(F&& ...f) const { return Overload(static_cast(f)...); } }; BOOST_HANA_INLINE_VARIABLE constexpr make_overload_t overload{}; #endif }} // end namespace boost::hana #endif // !BOOST_HANA_FUNCTIONAL_OVERLOAD_HPP