1

I want to call a lambda and pass the parameters separately.

e.g.:

#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.

1
  • I think this is what you're after? Commented Dec 31, 2020 at 13:27

1 Answer 1

3

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)...);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I missed that I still had the ... in there. FYI the return is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.