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:
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:
...
As far as I can tell from the grammar defined in the draft n3242, this code is valid C++11. A braced_init-list
is composed of a list of initializer-clause
, which can be assignment-expression
s or themselves braced_init_list
s. An assignment-expression
can be a lambda-expression
, which is exactly what you have as a first element ([...](...){...}
).
Therefore, surrounding the lambda with parentheses should not be required, if think you can safely file a bug report :). (Of course, this answer is based on a draft, so the possibility of a late change in the grammar is not to be excluded.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With