I use the svn-version of the gcc-4.7.0 to check out some C++11 features, e.g. Lambda Expressions. Since a couple of weeks some of my old examples including Lambdas to not compile anymore. I wonder:
- Did I miss a last-minute change in the C++11-Lambda-spec that has been implemented in gcc-4.7.0 in the last weeks?
- Is it a bug in gcc, that does not recognize inline-Lambdas anymore?
- Or did I misunderstood something else with Lambda-syntax?
The problematic code seems to involve inline-Lambdas that are provided as arguments directly.
Would you say that the following code is correct C++11 code?
#include <thread>
using namespace std;
struct Image {}; // dummy
void fill(int color, const Image& image) {
} // dummy
int main() {
int red;
Image img;
thread th{
[&img](int c){ fill(c, img); }, // error?
red };
th.join();
}
If I change it and assign the Lambda to a variable first it works:
#include <thread>
using namespace std;
struct Image {}; // dummy
void fill(int color, const Image& image) {
} // dummy
int main() {
int red;
Image img;
auto f = [&img](int c){ fill(c, img); }; // lambda
thread th{ f, red }; // ok now
th.join();
}
I put an example here where both compiles with gcc-4.5 (except that it raises an exception, probably because -pthread is not linked). But as I said: In my gcc-4.7.0-svn the first variant stopped compiling a couple of weeks ago.
Update The error message seems to be a parse error:
In function 'int main()':
...:30:11: error: expected '=' before '(' token
...:30:12: error: expected primary-expression before 'int'
...:30:12: error: expected ')' before 'int'
...:30:36: error: no matching function for call to
'std::thread::thread(<brace-enclosed initializer list>)'
...:30:36: note: candidates are:
...
thread th{ ([&img](int c){ fill(c, img); }), red };works. Hmmm... Should this be this way?