if

Description

The if_ function decorator makes the function callable if the boolean condition is true. The if_c version can be used to give a boolean condition directly(instead of relying on an integral constant).

When if_ is false, the function is not callable. It is a subtitution failure to call the function.

Synopsis

template<class IntegralConstant>
constexpr auto if_(IntegralConstant);

template<bool B, class F>
constexpr auto if_c(F);

Requirements

IntegralConstant must be:

  • IntegralConstant

F must be:

Example

#include <boost/hof.hpp>
#include <cassert>

struct sum_f
{
    template<class T>
    int operator()(T x, T y) const
    {
        return boost::hof::first_of(
            boost::hof::if_(std::is_integral<T>())(boost::hof::_ + boost::hof::_),
            boost::hof::always(0)
        )(x, y);
    }
};

int main() {
    assert(sum_f()(1, 2) == 3);
    assert(sum_f()("", "") == 0);
}

References