7

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

Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141

2 Answers2

11

Use templates:

template<typename Func>
inline void do_something_loop(Func f)
{
    for (int a=1; a<MAX_A; ++a) 
      for (int b=1; b<MAX_B; ++b) 
        for (int c=1; c<MAX_C; ++c) 
        { 
           f(a, b ,c); 
        } 
}

This can be called with any function pointer or function object that matches the signature, e.g.:

void do_something(int a, int b, int c) { /* stuff */ }

do_something_loop(do_something);

Or with a function object:

struct do_something
{
    void operator()(int a, int b, int c) { /* stuff */ }
};

do_something_loop(do_something()); 

Or if your compiler supports C++11, even with a lambda:

do_something_loop([](int a, int b, int c) { /* stuff */ });

Note that you could also declare the f parameter as a function pointer with the signature void(*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).

Sven
  • 21,903
  • 4
  • 56
  • 63
  • This is ok, but I need to define a function every time (no c++11). The advantages of macro is that I can write stuff like: `MYLOOP_BEGIN { v[a] = 10; vv[a][b][c] = a+1; } MYLOOP_END;` – Ruggero Turra Aug 23 '11 at 12:34
  • Yes, without lambdas there is no way other than a macro to do that. – Sven Aug 23 '11 at 12:57
3

Make a helper loop function:

template<typename Work>
void loop(Work work) {
  for (int a=1; a<MAX_A; ++a)
    for (int b=1; b<MAX_B; ++b)
      for (int c=1; c<MAX_C; ++c)
      {
        work(a, b ,c);
      }
}

Define the work to be done:

void some_work(int a, int b, int c) {
}

Then use:

loop(some_work);

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

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
  • 1/ `do` is a keyword (the specter of untested code...) 2/ The `&` before `some_work` is unneeded in C++, a function reference is trivially convertible into a function pointer. – Matthieu M. Aug 23 '11 at 09:15