Suppose I've this nested loop
for (int a=1; a<MAX_A; ++a)
for (int b=1; b<MAX_B; ++b)
for (int c=1; c<MAX_C; ++c)
{
do_something(a, b ,c);
}
and I reuse this loop in various part of my code, changing the function do_something
. It's quite boring to rewrite every time the first three lines. In python for example I would created a generator to return an iterator (1, 1, 1), (1, 1, 2), ...
or something like itertools.product
.
In c++ the only solution I've in mind is to define a macro. Something better?e
Make a helper loop function:
Define the work to be done:
Then use:
Note that now you don't need to use only function, but you can use a function object (object for which an
operator()
is overloaded).Use templates:
This can be called with any function pointer or function object that matches the signature, e.g.:
Or with a function object:
Or if your compiler supports C++11, even with a lambda:
Note that you could also declare the
f
parameter as a function pointer with the signaturevoid(*f)(int,int,int)
instead of using a template, but that's less flexible (it won't work on function objects (including the result of std::bind) or lambdas).