Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert function which ignores parameter in c++11?

I'm looking for a more elegant code to ignore needless parameter. Kind of reversing std::bind.

void doSomething(function<A(B)> callback); // Given function

// Instead of
void myDoSomething(function<A()> callback0) {
   doSomething([&](B b) { 
                  return callback0();
              });
}

// I want
void myDoSomething(function<A()> callback0) {
   doSomething(add_ignored_arg<B>(callback0));
}
like image 811
Erdemus Avatar asked Mar 18 '23 07:03

Erdemus


1 Answers

Option #1

C++11 basic solution, injecting an ignored parameter into the first place (DEMO 1):

template <typename Arg, typename R, typename... Args>
std::function<R(Arg, Args...)> add_ignored_arg(std::function<R(Args...)> f)
{
    return [f] (Arg&& /*arg*/, Args&&... args) { return f(std::forward<Args>(args)...); };
}

Note: std::forward<Args>(args) works correctly (although Args&& is not a forwarding-reference here) since std::function casts arguments passed by value to rvalue references, while lvalue references get collapsed according to the rule: & && -> &.

Option #2

C++14 basic solution with a generic lambda, injecting an ignored parameter into the first place (DEMO 2):

template <typename Arg, typename R, typename... Args>
std::function<R(Arg, Args...)> add_ignored_arg(std::function<R(Args...)> f)
{
    return [f] (auto&& /*arg*/, auto&&... args) { return f(std::forward<decltype(args)>(args)...); };
}

Option #3

C++11 advanced solution with injecting an ignored parameter into any place, with additional overloads that accept raw function pointers and std::function wrappers (DEMO 3):

template <std::size_t N, typename Arg, typename R, typename... Args, std::size_t... Before, std::size_t... After, typename Tuple = std::tuple<Args...>>
auto inject_param(index_sequence<Before...>, index_sequence<After...>)
    -> std::function<R(typename std::tuple_element<Before, Tuple>::type..., Arg, typename std::tuple_element<N + After, Tuple>::type...)>;

template <std::size_t N, typename Arg, typename R, typename... Args, std::size_t... Before, std::size_t... After, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(std::function<R(Args...)> f, index_sequence<Before...>, index_sequence<After...>)
    -> decltype(inject_param<N, Arg, R, Args...>(index_sequence<Before...>{}, index_sequence<After...>{}))
{
    return [f] (typename std::tuple_element<Before, Tuple>::type&&... before
                , Arg&& /*arg*/
                , typename std::tuple_element<N + After, Tuple>::type&&... after)
                {
                    return f(std::forward<typename std::tuple_element<Before, Tuple>::type>(before)...,
                             std::forward<typename std::tuple_element<N + After, Tuple>::type>(after)...);
                };
}

template <std::size_t N, typename Arg, typename R, typename... Args, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(std::function<R(Args...)> f)
    -> decltype(inject_param<N, Arg, R, Args...>(make_index_sequence<N>{}, make_index_sequence<sizeof...(Args) - N>{}))
{
    return add_ignored_arg<N, Arg>(f, make_index_sequence<N>{}, make_index_sequence<sizeof...(Args) - N>{});
}

template <std::size_t N, typename Arg, typename R, typename... Args, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(R(*f)(Args...))
    -> decltype(inject_param<N, Arg, R, Args...>(make_index_sequence<N>{}, make_index_sequence<sizeof...(Args) - N>{}))
{
    return add_ignored_arg<N, Arg>(std::function<R(Args...)>(f), make_index_sequence<N>{}, make_index_sequence<sizeof...(Args) - N>{});
}

Note: the implementation of index_sequence is in the demo. The forwarding works as described in Option #1.

Option #4

C++14 advanced solution with injecting an ignored parameter into any place, using a generic lambda and a return type deduction (DEMO 4):

template <std::size_t N, typename Arg, typename R, typename... Args, std::size_t... Before, std::size_t... After, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(std::function<R(Args...)> f, std::index_sequence<Before...>, std::index_sequence<After...>)
    -> std::function<R(typename std::tuple_element<Before, Tuple>::type..., Arg, typename std::tuple_element<N + After, Tuple>::type...)>
{
    return [f] (auto&&... args)
                {
                    auto t = std::forward_as_tuple(std::forward<decltype(args)>(args)...);
                    return f(static_cast<typename std::tuple_element<Before, decltype(t)>::type>(std::get<Before>(t))...,
                             static_cast<typename std::tuple_element<N + After + 1, decltype(t)>::type>(std::get<N + After + 1>(t))...);
                };
}

template <std::size_t N, typename Arg, typename R, typename... Args, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(std::function<R(Args...)> f)
{
    return add_ignored_arg<N, Arg>(f, std::make_index_sequence<N>{}, std::make_index_sequence<sizeof...(Args) - N>{});
}

template <std::size_t N, typename Arg, typename R, typename... Args, typename Tuple = std::tuple<Args...>>
auto add_ignored_arg(R(*f)(Args...))
{
    return add_ignored_arg<N, Arg>(std::function<R(Args...)>(f), std::make_index_sequence<N>{}, std::make_index_sequence<sizeof...(Args) - N>{});
}

Tests

struct B {};

void foo(int i, char c, double d)
{
    std::cout << i << " " << c << " " << d << std::endl;
}

void doSomething(std::function<void(int, char, B, double)> callback)
{
    callback(123, 'A', B{}, 3.14f);
}

void myDoSomething(std::function<void(int, char, double)> callback)
{
    doSomething(add_ignored_arg<2, B>(callback));
}

int main()
{
    myDoSomething(&foo);
}

Output:

123 A 3.14

Complete C++11 demo

Complete C++14 demo

like image 90
Piotr Skotnicki Avatar answered Apr 01 '23 14:04

Piotr Skotnicki