I am looking for a function that takes a couple of functions (e.g., callbacks) and their parameters, then run them with their given parameters. Input functions can have different number of parameters with various type. Please see below example code.
The following code does not compile and gives me a compilation error (on both gcc and clang with -std=c++17 compiler flag).
What I am trying to understand is this:
- Whether it is legal to have two parameter packs on a function?
- How compiler knows where to split parameters between two packs?
My 2nd question comes from the fact that in my use case, I am forwarding those parameter packs to other functions further down our software stack. So, at some point, I am consuming one of them, but rerouting the other function (with its parameters) to another function.
Please keep in mind that the functions/lambdas might not be in the same header file as the runFuncs function might be.
#include <sstream>
#include <string>
#include <iostream>
template <typename Func1, typename Func2,
typename... Func1Args,
typename... Func2Args>
void runFuncs(Func1 f1, Func2 f2, Func1Args&&... f1Args, Func2Args&&... f2Args) {
std::cout << f1(std::forward<Func1Args>(f1Args)...) << std::endl;
std::cout << f2(std::forward<Func2Args>(f2Args)...) << std::endl;
}
int main(int argc, char const *argv[]) {
auto lam1 = [](int a, float b) {
return a * b;
};
auto lam2 = [](std::string a, int n, float c) {
std::stringstream ss;
ss << ":::" << a << ":::" << n << ":::" << c << ":::";
return ss.str();
};
runFuncs(lam1, lam2, 2, 3.4f, "some_string", 555, 7.7f);
return 0;
}