4

I am trying to cin a loop index's value in the loop itself using lambda expression:

#include<iostream>
using namespace std;

int main(){
  for(int a, ([](int & b){cin>>b;})(a); a < 2; ++a);
  return 0;
}

These are the errors when i compile using g++ 4.5 on ubuntu:

forLoopAndCinTest.c++: In function ‘int main()’:
forLoopAndCinTest.c++:5:14: error: expected unqualified-id before ‘[’ token
forLoopAndCinTest.c++:5:14: error: expected ‘)’ before ‘[’ token
forLoopAndCinTest.c++:5:34: error: expected primary-expression before ‘)’ token
forLoopAndCinTest.c++:5:34: error: expected ‘;’ before ‘)’ token
forLoopAndCinTest.c++:5:40: error: name lookup of ‘a’ changed for ISO ‘for’ scoping
forLoopAndCinTest.c++:5:40: note: (if you use ‘-fpermissive’ G++ will accept your code)
forLoopAndCinTest.c++:5:50: error: expected ‘;’ before ‘)’ token

If i use a normal function instead of the lambda, program compiles fine.
Using -fpermissive doesnt help either.
Any ideas?

3
  • 1
    The current C++ standard doesn't have lambda expressions, so have you specified the option to enable C++0x support? Commented May 7, 2011 at 21:25
  • Oh yes Steve. I do use -std=C++0x flag when compiling. Plus, g++ complains if the flag is not used. Commented May 7, 2011 at 21:29
  • I dont think you capture variables in (). [] is used to capture variables. () is used to pass parameters to the lambda. Commented May 7, 2011 at 21:39

3 Answers 3

5

That's not how the for look works. You are trying to call a 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 the lambda, program compiles fine

Yes, but it's probably not doing what you think it does.

void f(int& b)
{
    cin >> b;
}

// ...
for( int a, f(a); a < 2; ++a );

Here, the loop declares two int variables, named a and f. The loop doesn't call f() as you might expect.

Try this instead:

for( int a; cin >> a && a < 2; ++a );
Sign up to request clarification or add additional context in comments.

3 Comments

I did not understand how the loop 'thinks' that a and f are variables and why f() isnt called. If it is not called, how is the cin statement executed? Thank you for the last idiom.
Got it. It doesnt execute the function at all. It just declares a variable with the appropriate type.
By the way i got what i wanted: for(int a, x = (cin >> a && 1); a < 10; ++a). Scans the variable inside the for loop although has to use an extra variable and the && operation. But cool. :)
2

The first part of the for is interpreted as a declaration. We get the very same error when replacing your code by the (almost) equivalent :

int main(){
    int a, ([](int & b){cin>>b;})(a); // This produces the same error
    for(; a < 2; ++a);
    return 0;
}

To answer a comment you made, for (int a, foo() ; ... works, but not like you think it does. It is in fact declaring a function (inside the for scope) that returns an int, and has the name foo. As in :

int a, foo();

Which you should read as :

int a;
int foo();

2 Comments

All this is to avoid the cin outside the loop. Yes, this will definitely work. But i still dont understand why i can call any normal function in the declaration part but not lambda.
You can't. When you write for (int a, foo() ; a<2 ; a++) you are NOT calling the foo function. (Try it for yourself).
0

After this: for( int a, compiler expects some name (of the variable) - unqualified-id. But in your case it is not so.

1 Comment

Yeah, it is just an expression list so your declaration should work either. Interesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.