Header lexy/callback/adapter.hpp

Adapt a function object or sink into a callback.

Callback lexy::callback and lexy::callback_with_state

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

    template <typename ReturnType = see-below, typename ... Fns>
    constexpr callback auto callback_with_state(Fns&&... fns);
}

Turns a regular invocable into a callback by specifying the return type, or creates an overload set of existing callbacks.

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.

If no ReturnType is specified and all fns are itself callbacks, it will be the common type of all their return types. Otherwise, it defaults to void.

callback_with_state will also pass the state passed via operator[] as the first argument to all calls to fns. lexy::callback_with_state(fn) is equivalent to lexy::bind (lexy::callback(fn), lexy::parse_state, lexy::values).

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