If you want int
and double
both, in the initialization, then one way to do this is to define an anonymous struct! Yes, you can define struct
in the for
loop, as well. It seems to be a less known feature of C++:
#include <iostream>
int main()
{
for( struct {int i; double j;} v = {0, 3.0}; v.i < 10; v.i++, v.j+=0.1)
std::cout << v.i << "," << v.j << std::endl;
}
Output:
0,3
1,3.1
2,3.2
3,3.3
4,3.4
5,3.5
6,3.6
7,3.7
8,3.8
9,3.9
Online demo : http://ideone.com/6hvmq