I have two similar functions. Both functions contain a nested for
-loop. How I can combine these two functions to decrease duplicated code.
The only difference between funcA
and funcB
is funcB
calls func_2
in the loop.
The two functions like below.
void funcA()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
func_1();
}
}
}
void funcB()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
func_1();
func_2();
}
}
}
You could have used variadic templates.
template<class ... FuncTypes>
void funcAB(FuncTypes... Funcs)
{
for(int i = 0; i < size; ++i) {
for(int j = 0; j < size; ++j) {
(Funcs(), ...);
}
}
}
Here is how to call the function.
funcAB(&func_1); // if you want to only call func_1
funcAB(&func_1, &func_2) // if you want both to be called
Perhaps I am taking it a little too far, but there is no apparent reason for the nested loops (neither func_1()
nor func_2()
depend on i
or j
). A straight forward way to pass a callable is the following:
template <typename F>
void func(F f) {
for (int i=0; i < size*size; ++i) f();
}
and then call either
func([](){ func_1(); });
func(&func_1); // passing function pointer works as well
or
func([](){ func_1(); func_2(); });
PS: There is a difference between the nested loops and the flat one when size*size
can overflow or it is negative. Though passing the callable is orthogonal to that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With