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).
See Question&Answers more detail:os