I need a function My_func that works like this
auto f = [](string s, double c) { return c; };
assert(My_func(f)(std::make_tuple("Hello", 8.5)) == f("Hello", 8.5'));
Now i have
template <class T>
auto My_func(T&& f) {
return [f = std::forward<T>(f)](auto&& value) {
};
}
What should i add?
CodePudding user response:
I believe you are looking for std::apply
#include <cassert>
#include <string>
#include <tuple>
template <class F>
constexpr auto My_func(F&& f) {
return [f = std::forward<F>(f)](auto&& tuple) mutable {
return std::apply(std::forward<decltype(f)>(f),
std::forward<decltype(tuple)>(tuple));
};
}
int main() {
auto f = [](std::string s, double c) { return c; };
assert(My_func(f)(std::make_tuple("Hello", 8.5)) == f("Hello", 8.5));
}
This is essentially just a functor wrapper for std::apply
, that is still useful because it would not be possible to pass it around due to it being a template.