Header lexy/callback/adapter.hpp

Adapt a function object or sink into a callback.

Callback lexy::callback

lexy/callback/adapter.hpp
namespace lexy
{
    template <typename ReturnType = void, typename ... Fns>
    constexpr callback auto callback(Fns&&... fns);
}

Turns a regular invocable into a callback by specifying the return type.

The returned callback will take arbitrary arguments and perform overload resolution on the fns, which must be invocables. The result must return an object of the specified ReturnType.

Example 1. Build a callback from lambdas
constexpr auto my_callback
    // The return type is int.
    = lexy::callback<int>([](int i) { return 2 * i; }, //
                          [](int a, int b) { return a + b; });

Callback lexy::callback (sink overload)

lexy/callback/adapter.hpp
namespace lexy
{
    constexpr callback auto callback(sink<> auto&& sink);
}

Turns a sink into a callback.

The resulting callback will take arbitrary arguments. It will first obtain the sink callback of sink, then pass each argument to it in a separate invocation. The finished result is returned by the callback.

Example 2. Turn a sink into a callback
constexpr auto my_callback = lexy::callback(lexy::fold<int>(0, std::plus<int>{}));
Note
See lexy::collect for the inverse operation that turns a callback into a sink.

Callback lexy::mem_fn

lexy/callback/adapter.hpp
namespace lexy
{
    template <typename Member, typename ClassT>
    constexpr callback auto mem_fn(Member ClassT::* mem_ptr);
}

Turns a member function or data pointer into a callback.

It is equivalent to lexy::callback<ReturnType>(mem_ptr), where ReturnType is deduced from the type of mem_ptr.

Note
mem_fn is not necessary for passing a member function pointer to e.g. lexy::callback; they all use INVOKE internally.

See also