This is not what the for looks like. You are trying to call lambda where the compiler expects you to declare an int :
for( int a, int2, ...; a < 2; ++a );
Now,
If I use a normal function instead of lambda, the program compiles fine
Yes, but he probably doesn't do what you think he does.
void f(int& b) { cin >> b; }
Here the loop declares two int variables named a and f . The loop does not call f() , as you might expect.
Try this instead:
for( int a; cin >> a && a < 2; ++a );
source share