Search code examples
c++for-loopscopeinitializervariable-declaration

Is there a way to define variables of two different types in a for loop initializer?


You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

But it is illegal to define variables of different types:

int main() {
  for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

Is there a way to do this? (I don't need to use i inside the loop, just j.)

If you have totally hacked and obscure solution, It's OK for me.

In this contrived example I know you could just use double for both variables. I'm looking for a general answer.

Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach macro:

#define foreach(var, iter, instr) {                  \
    typeof(iter) var##IT = iter;                     \
    typeof(iter)::Element var = *var##IT;            \
    for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) {  \
      instr;                                         \
    }                                                \
  }

It can be used thus:

foreach(ii, collection, {
  cout << ii;
}). 

But I need something that will be used like that:

foreach(ii, collection)
  cout << ii;

Please do not introduce any runtime overhead (but it might be slow to compile).


Solution

  • Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see @kitchen's one above):

    FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) { 
    
    }
    

    The first part specifies a sequence of declarations: (a)(b).... The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.

    There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:

    if(int k = 0) ; else COMPOUND_STATEMENT
    

    Then k is visible. Naturally, it always have to evaluate to false. So it can't be used by us. The other context is this one:

    for(int k = 0; ...; ...) COMPOUND_STATEMENT
    

    That's what i'm going to use here. We'll have to watch to only make one iteration of COMPOUND_STATEMENT. The actual for loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.

    #include <boost/preprocessor.hpp>
    #include <iostream>
    
    #define EMIT_DEC_(R,D,DEC) \
        for(DEC; !_k; ) 
    
    #define FOR(DECS, COND, INC) \
        if(bool _k = false) ; else \
          BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS) \
            for(_k = true; COND; INC)
    
    int main() {
        FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
        }
    }
    

    It's creating a bunch of for statements, each nested into another one. It expands into:

    if(bool _k = false) ; else
      for(int i = 0; !_k; )
        for(float j = 0.0f; !_k; )
          for(_k = true; i < 10; (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
          }