capture¶
Header¶
#include <boost/hof/capture.hpp>
Description¶
The capture
function decorator is used to capture values in a function.
It provides more flexibility in capturing than the lambda capture list in
C++. It provides a way to do move and perfect capturing. The values
captured are prepended to the argument list of the function that will be
called.
Synopsis¶
// Capture by decaying each value
template<class... Ts>
constexpr auto capture(Ts&&... xs);
// Capture lvalues by reference and rvalue reference by reference
template<class... Ts>
constexpr auto capture_forward(Ts&&... xs);
// Capture lvalues by reference and rvalues by value.
template<class... Ts>
constexpr auto capture_basic(Ts&&... xs);
Semantics¶
assert(capture(xs...)(f)(ys...) == f(xs..., ys...));
Example¶
#include <boost/hof.hpp>
#include <cassert>
struct sum_f
{
template<class T, class U>
T operator()(T x, U y) const
{
return x+y;
}
};
int main() {
auto add_one = boost::hof::capture(1)(sum_f());
assert(add_one(2) == 3);
}