How do I assign a lambda as default argument? I would like to do this:
int foo(int i, std::function<int(int)> f = [](int x) -> int { return x / 2; }) { return f(i); } but my compiler (g++ 4.6 on Mac OS X) complains:
error: local variable 'x' may not appear in this context EDIT: Indeed, this was a compiler bug. The above code works fine with a recent version of gcc (4.7-20120225).
error C2587: 'x' : illegal use of local variable as default parameter-see declaration of 'x'. It seems to because of the parameter, if I remove it from the lambda,std::functionand function call, it compiles perfectly fine.clang++ -c -std=c++11