I have this code inherited from old C++ time using macros. I'm currently replacing it, and I'm at the point where some constructs need to be factored.
Typically, I have this:
if(condition)
{
fun1(fun2(arguments, arg1)); // let's say arg1 is a vector of doubles
}
else
{
fun1(fun2(arguments, arg2)); // let's say arg2 is a double
}
several times. fun1()
has a different set of arguments depending on fun2()
argument types, and I could have arg1
and arg2
there as well (the true code actually has several layers of if
s with each time a different set of types, several additional layers of functions inside each branch).
I'd like to factor this one out in a function that can take a template lambda like this:
[&](auto arg) { fun1(fun2(arguments, arg));}
Now, the issue is that this is templated, so I can't turn it into a std::function
, so I don't know what kind of argument I should use to create my function:
void dispatch(bool condition, const std::vector<double>& arg1, double arg2, ???? lambda)
{
if(condition)
{
lambda(arg1);
}
else
{
lambda(arg2);
}
}
Is there such an option in C++17? or even C++20?