1

NOTE: I know I can move the declaration outside the loop.

I want to declare a couple of variables in a for loop:

for ( int x = 0, int y = 0 ; ; )
{
}

,but this doesn't work since I can't specify a type after the comma ,. In this case, removing the second int or declaring y outside the loop would fix the problem, but what if I want to declare both variables inside the loop and also have different types?

Can I have something like:

for ( int x = 0, float y = 0 ; ; )
{
}

?

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • possible duplicate of [Is there a way to define variables of two types in for loop?](http://stackoverflow.com/questions/866012/is-there-a-way-to-define-variables-of-two-types-in-for-loop) – Hasturkun Jan 04 '12 at 14:38
  • 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

5 Answers5

8

This is impossible; the C++ grammar just won't admit it. The closest you can get to this is putting an extra scope around the loop:

{
    int x;
    float y;

    for (x=0, y=0;;) {
    }
}
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • As pointed out in other answers you can do it by declaring a struct type inside the for statement, or using another aggregate type like std::tuple. – bames53 Jan 04 '12 at 16:56
  • @bames53: yes, but strictly, that won't give you more than one *variable* with scope restricted to the `for` loop. – Fred Foo Jan 04 '12 at 16:58
5

no, you can only declare variables of one type in there. What you could do is work around this issue with std::pair, std::touple or some similar construct:

for(std::pair<int, float> p = std::make_pair(0, 0.0f);; )
{
    p.first++;
    p.second *= 0.5f;
}
Fiktik
  • 1,941
  • 16
  • 25
  • I was also going to mention `pair`/`touple`. I would still caution that this seems like a strange use of a `for` statement. If one of the variables isn't specifically used for constraining the iteration, it shouldn't be mashed into the `for`. – Bret Kuhns Jan 04 '12 at 14:41
  • I agree, although this is a way, its not a way I would normally use. It doesn't help with the readability of the loop body and the construct seems very unusual when glancing through code. I'd go with solution that @larsmans proposes. – Fiktik Jan 04 '12 at 14:51
  • You can also declare a struct type with members. See my answer. – bames53 Jan 04 '12 at 16:54
2

C++ allows you to do this:

for( struct {int x; float y;} s; s.x<10; s.x++,s.y*=2.0f) {

}

MSVC has a bug such that it does not allow this, but more standards compliant compilers allow it.

bames53
  • 86,085
  • 15
  • 179
  • 244
0

No, that's not possible, they all have to be of the same type.

m0e
  • 1
  • 1
0

You can't.

My suggestion would be to split the code inside into a separate function to keep it readable:

template<typename O, typename I>
O copy(I in, I end, O out) {
    for(; in != end; ++in, ++out)
        *out = *in;
    return out;
}

IMO, this is much nicer than inventing a new scope or extending the lifetime of iterators, plus it makes you think whether the code can be genericized.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64