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));
}
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: & &&
-> &
.
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)...); };
}
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.
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>{});
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With