Edit: /usr/include/boost/hof/first_of.hpp (7100B)
/*=============================================================================
Copyright (c) 2012 Paul Fultz II
first_of.h
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_HOF_GUARD_FUNCTION_CONDITIONAL_H
#define BOOST_HOF_GUARD_FUNCTION_CONDITIONAL_H
/// first_of
/// ========
///
/// Description
/// -----------
///
/// The `first_of` function adaptor combines several functions together. If
/// the first function can not be called, then it will try to call the next
/// function. This can be very useful when overloading functions using
/// template constraints(such as with `enable_if`).
///
/// Note: This is different than the [`match`](match.md) function adaptor, which
/// can lead to ambiguities. Instead, `first_of` will call the first function
/// that is callable, regardless if there is another function that could be
/// called as well.
///
/// Synopsis
/// --------
///
/// template
/// constexpr first_of_adaptor first_of(Fs... fs);
///
/// Requirements
/// ------------
///
/// Fs must be:
///
/// * [ConstInvocable](ConstInvocable)
/// * MoveConstructible
///
/// Example
/// -------
///
/// #include
/// #include
/// using namespace boost::hof;
///
/// struct for_ints
/// {
/// void operator()(int) const
/// {
/// printf("Int\n");
/// }
/// };
///
/// struct for_floats
/// {
/// void operator()(float) const
/// {
/// printf("Float\n");
/// }
/// };
///
/// int main() {
/// first_of(for_ints(), for_floats())(3.0);
/// }
///
/// This will print `Int` because the `for_floats` function object won't ever be
/// called. Due to the conversion rules in C++, the `for_ints` function can be
/// called on floats, so it is chosen by `first_of` first, even though
/// `for_floats` is a better match.
///
/// So, the order of the functions in the `first_of_adaptor` are very important
/// to how the function is chosen.
///
/// References
/// ----------
///
/// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
/// Proposal for C++ generic overload function
/// * [Conditional overloading]()
///
#include
#include
#include
#include
#include
#include
#include
#include
namespace boost { namespace hof {
namespace detail {
template
struct basic_first_of_adaptor : F1, F2
{
BOOST_HOF_INHERIT_DEFAULT(basic_first_of_adaptor, F1, F2)
template
constexpr basic_first_of_adaptor(A&& f1, B&& f2)
noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F1, A&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F2, B&&))
: F1(BOOST_HOF_FORWARD(A)(f1)), F2(BOOST_HOF_FORWARD(B)(f2))
{}
template::type>
constexpr basic_first_of_adaptor(X&& x)
BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F1, X&&)
: F1(BOOST_HOF_FORWARD(X)(x))
{}
template
struct select
: std::conditional
<
is_invocable::value,
F1,
F2
>
{};
BOOST_HOF_RETURNS_CLASS(basic_first_of_adaptor);
template::type>
constexpr BOOST_HOF_SFINAE_RESULT(typename select::type, id_...)
operator()(Ts && ... xs) const
BOOST_HOF_SFINAE_RETURNS
(
BOOST_HOF_RETURNS_STATIC_CAST(const F&)(*BOOST_HOF_CONST_THIS)(BOOST_HOF_FORWARD(Ts)(xs)...)
);
};
template
constexpr const F1& which(std::true_type, const F1& f1, const F2&) noexcept
{
return f1;
}
template
constexpr const F2& which(std::false_type, const F1&, const F2& f2) noexcept
{
return f2;
}
template
struct conditional_kernel : compressed_pair
{
typedef compressed_pair base;
BOOST_HOF_INHERIT_CONSTRUCTOR(conditional_kernel, base)
template
struct select
: std::conditional
<
is_invocable::value,
F1,
F2
>
{};
BOOST_HOF_RETURNS_CLASS(conditional_kernel);
template>
constexpr BOOST_HOF_SFINAE_RESULT(typename select::type, id_...)
operator()(Ts && ... xs) const
BOOST_HOF_SFINAE_RETURNS
(
boost::hof::detail::which(
BOOST_HOF_RETURNS_CONSTRUCT(PickFirst)(),
BOOST_HOF_MANGLE_CAST(const F1&)(BOOST_HOF_CONST_THIS->first(xs...)),
BOOST_HOF_MANGLE_CAST(const F2&)(BOOST_HOF_CONST_THIS->second(xs...))
)
(BOOST_HOF_FORWARD(Ts)(xs)...)
);
};
}
template
struct first_of_adaptor
: detail::conditional_kernel
{
typedef first_of_adaptor fit_rewritable_tag;
typedef BOOST_HOF_JOIN(first_of_adaptor, Fs...) kernel_base;
typedef detail::conditional_kernel base;
BOOST_HOF_INHERIT_DEFAULT(first_of_adaptor, base)
template
constexpr first_of_adaptor(X&& f1, Xs&& ... fs)
noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base, X&&, kernel_base) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(kernel_base, Xs&&...))
: base(BOOST_HOF_FORWARD(X)(f1), kernel_base(BOOST_HOF_FORWARD(Xs)(fs)...))
{}
template
constexpr first_of_adaptor(X&& f1)
BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base, X&&)
: base(BOOST_HOF_FORWARD(X)(f1))
{}
struct failure
: failure_for
{};
};
template
struct first_of_adaptor : F
{
typedef first_of_adaptor fit_rewritable_tag;
BOOST_HOF_INHERIT_CONSTRUCTOR(first_of_adaptor, F);
struct failure
: failure_for
{};
};
template
struct first_of_adaptor
: detail::conditional_kernel
{
typedef detail::conditional_kernel base;
typedef first_of_adaptor fit_rewritable_tag;
BOOST_HOF_INHERIT_CONSTRUCTOR(first_of_adaptor, base);
struct failure
: failure_for
{};
};
BOOST_HOF_DECLARE_STATIC_VAR(first_of, detail::make);
}} // namespace boost::hof
#endif