This works:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
` But this not:
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 define variables of two types in for loop? I don't need to use iterator i inside the loop, just j.
If you have totally hacked and obscure solution, It's OK for me.
Please do not give suggestions to use float for both i and j, in my real problem i is my own iterator (not STL) to value of type j.
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 and the for statement is to be enclosed in foreach macro.
I have this 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; \
} \
}
I can be used like that:
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).
Thank you in advance. :)