1 /*=============================================================================
2 Copyright (c) 2015 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_IF_H
9 #define BOOST_HOF_GUARD_IF_H
17 /// The `if_` function decorator makes the function callable if the boolean
18 /// condition is true. The `if_c` version can be used to give a boolean
19 /// condition directly(instead of relying on an integral constant).
21 /// When `if_` is false, the function is not callable. It is a subtitution
22 /// failure to call the function.
27 /// template<class IntegralConstant>
28 /// constexpr auto if_(IntegralConstant);
30 /// template<bool B, class F>
31 /// constexpr auto if_c(F);
36 /// IntegralConstant must be:
38 /// * IntegralConstant
42 /// * [ConstInvocable](ConstInvocable)
43 /// * MoveConstructible
48 /// #include <boost/hof.hpp>
49 /// #include <cassert>
54 /// int operator()(T x, T y) const
56 /// return boost::hof::first_of(
57 /// boost::hof::if_(std::is_integral<T>())(boost::hof::_ + boost::hof::_),
58 /// boost::hof::always(0)
64 /// assert(sum_f()(1, 2) == 3);
65 /// assert(sum_f()("", "") == 0);
71 /// * [static_if](static_if)
74 #include <boost/hof/always.hpp>
75 #include <boost/hof/detail/callable_base.hpp>
76 #include <boost/hof/detail/forward.hpp>
77 #include <boost/hof/detail/delegate.hpp>
78 #include <boost/hof/detail/move.hpp>
79 #include <boost/hof/detail/static_const_var.hpp>
81 namespace boost { namespace hof {
85 template<class C, class...>
90 template<bool Cond, class F>
91 struct if_adaptor : detail::callable_base<F>
93 BOOST_HOF_INHERIT_CONSTRUCTOR(if_adaptor, detail::callable_base<F>)
97 struct if_adaptor<false, F>
100 constexpr if_adaptor(Ts&&...) noexcept
107 constexpr make_if_f() noexcept
110 constexpr if_adaptor<Cond, F> operator()(F f) const BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
112 return if_adaptor<Cond, F>(static_cast<F&&>(f));
120 template<class Cond, bool B=Cond::type::value>
121 constexpr make_if_f<B> operator()(Cond) const noexcept
128 #if BOOST_HOF_HAS_VARIABLE_TEMPLATES
130 BOOST_HOF_STATIC_CONSTEXPR detail::make_if_f<B> if_c = {};
132 template<bool B, class F>
133 constexpr detail::if_adaptor<B, F> if_c(F f) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
135 return detail::if_adaptor<B, F>(static_cast<F&&>(f));
139 BOOST_HOF_DECLARE_STATIC_VAR(if_, detail::if_f);
141 }} // namespace boost::hof