Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template function for applying args to function

I trying to write a function (call it apply_args()) that takes a specific function or function object and arguments for calling this object, and calls it using the perfect forwarding.

Example:

auto fun = [](std::string a, std::string const& b) { return a += b; };

std::string s("world!");

// s is passing by lvalue ref,
// temporary object by rvalue ref 
s = apply_args(fun, std::string("Hello, "), s);

How can I implement that function?

like image 238
user1786089 Avatar asked Sep 16 '26 08:09

user1786089


1 Answers

template <typename Func, typename ...Args>
decltype(auto) apply_args(Func &&f, Args &&...args) {
    return f(std::forward<Args>(args)...);
}
like image 62
Andrei R. Avatar answered Sep 19 '26 05:09

Andrei R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!