7

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

Mechanical snail
  • 26,499
  • 14
  • 83
  • 107
Łukasz Lew
  • 43,526
  • 39
  • 134
  • 202
  • 1
    You do realize that there might not be any solution that satisfies all the conditions you've given? – David Z May 14 '09 at 21:51
  • 6
    "Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop." It sounds like you have created an artificial requirement. What you care about is the scope of the iterator, not whether it is in the body of the for. Try to explain *only* your requirements without adding your preconceived notions about scoping. It will be easier for people to understand your problem and help you. – dss539 May 14 '09 at 22:01
  • 3
    everytime someone comes up with a solution, you add a "Please do not say ....." and comment "not useful... changes body...." Why do you do that?? – Johannes Schaub - litb May 14 '09 at 22:04
  • your comments are very good, sorry for my bad question. I will clarify it in a moment. – Łukasz Lew May 14 '09 at 22:13
  • Would it be possible for you to not use a foreach macro, and just use two for loops? – xian May 14 '09 at 22:15
  • Please do not say i is an int if it is not. And is j really a float? – Zifre May 14 '09 at 22:18
  • 1
    Please describe your for-each stuff. I'm sure there are ways out of it. boost preprocessor comes to mind. Like you could do FOREACH((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) { ... } would be an invented, possible syntax. or do you mean an enclosing for-each? kitchen's solution would still work then. { ... } are perfectly fine as statements where for is allowed. – Johannes Schaub - litb May 14 '09 at 22:28
  • Is (int i = 0)(int j = 0.0) some special boost syntax? (T1 i = 0)(T2 j = f(i)) will work in FOREACH ? – Łukasz Lew May 14 '09 at 22:36
  • 5
    "But I need something that will be used like that: foreach(ii, collection) cout << ii; ". That's the biggest BS I have seen for a long time. Stop making up any new artificial requirements. And by the way the foreach macro crap has nothing to do with the question about defining 2 variables in a for loop. – lothar May 14 '09 at 22:52
  • 1
    Uhm... simplest question ever: #include , then use: BOOST_FOREACH( int & x, container ) – David Rodríguez - dribeas May 14 '09 at 23:04
  • @Lukasz, just enhance boost::range so it supports your container/collection, and it will work automatically with boost-foreach. also, your collection is strange: why is it that it has the same type as one of its iterators? – Johannes Schaub - litb May 14 '09 at 23:16
  • seriously, this is starting to look like a joke question :) – Johannes Schaub - litb May 14 '09 at 23:16
  • possible duplicate of [Can I declare variables of different types in the initialization of a for loop?](http://stackoverflow.com/questions/8644707/can-i-declare-variables-of-different-types-in-the-initialization-of-a-for-loop) – Mechanical snail Aug 17 '12 at 05:24

13 Answers13

26

Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop.

You could do this:

#include <iostream>

int main( int, char *[] ) {
    {
        float j = 0.0;

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

    float j = 2.0; // works

    std::cout << j << std::endl;

    return 0;
}
xian
  • 4,457
  • 4
  • 32
  • 38
  • 11
    Why is it not useful? It satisfies all your requirements. – Michael Myers May 14 '09 at 22:00
  • 2
    @Łukasz: Why not? Which one of your conditions does this violate? – Reunanen May 14 '09 at 22:00
  • 2
    Given all of the conditions you want met, I don't think there is a more practical solution. – xian May 14 '09 at 22:02
  • 3
    @Łukasz: The artificial scope forces j to be `destroyed` at scope end. It's identical to having j defined at the same time as i. – Naaff May 14 '09 at 22:02
  • 1
    What would make the { ... } proposed by kitchen stop working when used together with "for-each" (what for-each anyway? boost or qt his own?) – Johannes Schaub - litb May 14 '09 at 22:16
  • 1
    @litb: Good question! Also, if it won't work with the foreach, then maybe he should consider using another for loop. – xian May 14 '09 at 22:18
  • 1
    I have no idea why this superior answer suggesting to simply open and close a temporary scope languishes without a checkmark, while the OP somehow thinks that parodying a scope using multiple redundant `for` loops is the real solution. The ability to open and close scopes exists for a reason. It's things like this. `for` loops exist to loop, not to be stuck together in crude, pointless approximations of a temporary scope. – underscore_d May 25 '17 at 23:20
11

Well, it's ugly. But you could use pair.

int main() {
  for (std::pair<int,float> p(0,0.0f); 
       p.first < 10; 
       p.first += 1, p.second = 2*p.first) {
    cout << p.second << endl;
  }
}
Chris Jester-Young
  • 206,112
  • 44
  • 370
  • 418
Mike G.
  • 1,650
  • 11
  • 17
10

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;
      }
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
8
{
  int i = 0;
  float j = 0.0;
  for ( ; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

The variables "disappear" after the block.

Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
7

This will make the iterator (or in this case, float) disappear when it's no more needed:

int main() {
  // some code...

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

  // more code...
}
Reunanen
  • 7,551
  • 2
  • 31
  • 55
6

If you're having trouble with macros, there's a standard do..while trick that works perfectly:

#define MYFOR(init, test, post, body) \
    do \
    { \
        init \
        for( ; test; post) \
            body \
    } while(0)

Use it as follows:

MYFOR( int i = 0; float j = 0.0f; , i < 10 , (i += 1, j = 2.0f * i),
    {
         cout << j << endl;
    } );

It's ugly, but it does what you want: the scope of i and j is limited by the do..while loop from the macro, and it requires a semicolon at the end, so you won't get bitten by putting it in the predicate of an if/else statement.

Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
5

This one is also ugly, but provides also some general way for declaring multiple variables with some given name and types in a for-loop

int main() {
  for (struct { int i; float j; } x = { };
       x.i < 10; x.i += 1, x.j = 2 * x.i) {
    cout << x.j << endl;
  }
}
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
4

EDIT: Question has changed once more. The question now explicitly wants to implement a foreach loop. The simplest answer:

#include <boost/foreach.hpp>
void( std::vector<int>& v ) {
   BOOST_FOREACH( int & x, v ) {
      x = x*2;
   }
}

Injecting a variable into a code block

This is not intended as an answer, but to show a more general technique for injecting a variable into a code block. It seems as if the macro the OP is trying to define might use, even if it does incur in some overhead

There are a couple of places where you can define a variable with different scopes. You can define a variable inside any code block, and its lifespan will be to the end of that particular block. You can define a variable in the parenthesis of a for loop and the scope will be the loop block. You can also define a variable inside an if block and its scope will be that of the if (including the else clause).

You can combine those options above to create externally and inject variables into a code block without creating a variable whose lifespan exceeds that of the block. A practical example would be defining a foreach loop (simplified to work only on STL containers. The calling syntax would be:

void f( std::vector<int>& container ) 
{
   INTVECTOR_FOREACH( int & x, container )
   {
      x = x*2;
   }
}

With semantics similar to foreach in other languages: x gets referenced to each element in the container, so that the function actually doubles each value inside the integer vector.

Now the code of the simplified macro:

#define INTVECTOR_FOREACH( variable, container ) \
   for ( std::vector<int>::iterator it = container.begin(); it!=container.end(); ++it ) \
      if ( bool condition=false ) {} else \
         for ( variable = *it; !condition; condition=true )

Generalizing the macro for any container and type requires some metaprogramming that falls out of the context of the question, but the idea of how it works (I hope) should not be too hard to follow.

The external for iterates over the container, in each iteration we execute another for only once defining the iterating variable (int & x in the sample code). We need a condition to control the number of iterations (1) of the internal loop, and that condition is injected with an if. We choose to make the if fail so that we can ensure that the user does not get unexpected results if she writes an else after the loop... macros are tricky.

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
  • But this breaks **break** and **continue**. Note that you can declare the variable directly in the `if` part and it will work. Your variable must test to true. – xryl669 Apr 14 '21 at 11:08
3

Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop.

You could still do that, and put the whole thing in curly braces to make the extra variable go out of scope.

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

This way j would go out of scope right after the loop.

Mechanical snail
  • 26,499
  • 14
  • 83
  • 107
Dima
  • 37,098
  • 13
  • 69
  • 112
2

With the requirements you give the simplest code I can think of is:

for ( int i = 0; i < 10; ++i )
{
   float f = i * 2;
   std::cout << f << std::endl;
}

You only use f as twice the value of i. The lifetime is limited to the loop and (at least in the simplified question you provide) floats are cheap to create (exactly as cheap as to assign).

If construction of the real float (I am assuming that since i is not really an int, f may not be a float either) is much more expensive than reassigning the value, then the other solutions of encapsulating inside an extra pair of curly braces to limit scope would be the best option.

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
1
int main() {
  for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

Maybe I'm being dense, but why do you even have to declare the float? You just "throw it away" when you leave the loop anyways. Right?

for(int i=0; i<10; ++i)
    cout << (float)2*i << endl;

Why do you need j?

dss539
  • 6,410
  • 2
  • 31
  • 61
1

You say the i is your own type, and you just need to generate j out of i, right? Easy. Add a member function to i's class to generate the j value, and use that always. You can probably even make a macro to "hide" the call to that member function, if you want. :-)

Chris Jester-Young
  • 206,112
  • 44
  • 370
  • 418
0

Why don't you just declare and initialize your variables outside of the for loop? You can still test and increment as you have it now.

Filip Frącz
  • 5,651
  • 11
  • 42
  • 65
  • Because for statement will be used in macro. – Łukasz Lew May 14 '09 at 22:03
  • Well, there's your problem. Macros are inherently flawed and dangerous and you run into conditions they can't handle. For that matter, you are quite often screwed because macros don't understand templates with commas in them. Give up on the macro, all your problems go away. – Matt Cruikshank May 14 '09 at 22:59
  • As many have already mentioned, just enclose your expression within a new scope { .. }. Pozdrawiam. – Filip Frącz May 15 '09 at 03:18
  • @FilipFrącz: Extra indentation looks so ugly. – Rainning Feb 20 '21 at 09:08