hpx::function_ref

hpx::function_ref#

Defined in header hpx/functional.hpp.

See Public API for a list of names and headers that are part of the public HPX API.

template<typename Sig>
class function_ref#

function_ref class is a vocabulary type with reference semantics for passing entities to call.

An example use case that benefits from higher-order functions is retry(n,f) which attempts to call f up to n times synchronously until success. This example might model the real-world scenario of repeatedly querying a flaky web service.

using payload = std::optional< /* ... */ >;
// Repeatedly invokes `action` up to `times` repetitions.
// Immediately returns if `action` returns a valid `payload`.
// Returns `std::nullopt` otherwise.
payload retry(size_t times, /* ????? */ action);
The passed-in action should be a callable entity that takes no arguments and returns a payload. This can be done with function pointers, hpx::function or a template but it is much simpler with function_ref as seen below:
payload retry(size_t times, function_ref<payload()> action);