BOOST_HOF_LIFT

Description

The macros BOOST_HOF_LIFT and BOOST_HOF_LIFT_CLASS provide a lift operator that will wrap a template function in a function object so it can be passed to higher-order functions. The BOOST_HOF_LIFT macro will wrap the function using a generic lambda. As such, it will not preserve constexpr. The BOOST_HOF_LIFT_CLASS can be used to declare a class that will wrap function. This will preserve constexpr and it can be used on older compilers that don’t support generic lambdas yet.

Limitation

In C++14, BOOST_HOF_LIFT doesn’t support constexpr due to using a generic lambda. Instead, BOOST_HOF_LIFT_CLASS can be used. In C++17, there is no such limitation.

Synopsis

// Wrap the function in a generic lambda
#define BOOST_HOF_LIFT(...)

// Declare a class named `name` that will forward to the function
#define BOOST_HOF_LIFT_CLASS(name, ...)

Example

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

// Declare the class `max_f`
BOOST_HOF_LIFT_CLASS(max_f, std::max);

int main() {
    auto my_max = BOOST_HOF_LIFT(std::max);
    assert(my_max(3, 4) == std::max(3, 4));
    assert(max_f()(3, 4) == std::max(3, 4));
}