I want to call a lambda and pass the parameters separately.
eg:
#include <memory>
template<typename T, typename... TS>
T call(T (*)(TS...) f, TS&&... args) {
return f(std::forward<TS...>(args...));
}
Thus I want to call this function like this:
call([](auto arg1, auto arg2){
std::cout << arg1 << ", " << arg2 << std::endl;
}, 1, 2);
This should print out 1, 2
.
You can't just slap ...
everywhere and hope it works. Understand how parameter packs work and use the correct syntax. Furthermore, the function call()
should not return T
. Use auto
for the return type. And T
is already the full type of f
, you should not write T (*)(TS...)
. Here is the fixed version:
template<typename T, typename... TS>
auto call(T f, TS&&... args) {
return f(std::forward<TS>(args)...);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.