Lambda语法或gcc bug的最后一分钟更改?

时间:2021-08-01 13:07:04

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:

我使用gcc-4.7.0的svn版本来检查一些c++ 11特性,例如Lambda表达式。从几周以来,我的一些旧示例(包括Lambdas)不再编译。我想知道:

  • 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?
  • 我是否错过了前几周在gcc-4.7.0中实现的c++ 11- lambda规范的最后一分钟更改?
  • Is it a bug in gcc, that does not recognize inline-Lambdas anymore?
  • 它是gcc中的一个bug,不再识别内联- lambdas吗?
  • Or did I misunderstood something else with Lambda-syntax?
  • 还是我用lambda语法误解了别的东西?

The problematic code seems to involve inline-Lambdas that are provided as arguments directly.

有问题的代码似乎涉及到直接作为参数提供的内行- lambdas。

Would you say that the following code is correct C++11 code?

你说下面的代码是正确的c++ 11代码吗?

#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.

我在这里放了一个示例,其中两个都使用gcc-4.5编译(除了它引发了一个异常,可能是因为-pthread没有链接)。但是正如我所说:在我的gcc-4.7.0-svn中,第一个版本在几周前就停止了编译。

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:
           ...

1 个解决方案

#1


3  

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-expressions or themselves braced_init_lists. An assignment-expression can be a lambda-expression, which is exactly what you have as a first element ([...](...){...}).

从n3242草案中定义的语法可以看出,该代码是有效的c++ 11。braced_init-list由initializer-子句的列表组成,可以是赋值表达式,也可以是它们自己braced_init_lists。赋值表达式可以是lambda表达式,这正是您作为第一个元素([…](…)(){…})所拥有的。

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.)

因此,如果您认为可以安全地提交bug报告:),就不需要使用括号包围lambda。(当然,这个答案是基于一份草稿,所以不能排除语法有晚些变化的可能性。)

#1


3  

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-expressions or themselves braced_init_lists. An assignment-expression can be a lambda-expression, which is exactly what you have as a first element ([...](...){...}).

从n3242草案中定义的语法可以看出,该代码是有效的c++ 11。braced_init-list由initializer-子句的列表组成,可以是赋值表达式,也可以是它们自己braced_init_lists。赋值表达式可以是lambda表达式,这正是您作为第一个元素([…](…)(){…})所拥有的。

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.)

因此,如果您认为可以安全地提交bug报告:),就不需要使用括号包围lambda。(当然,这个答案是基于一份草稿,所以不能排除语法有晚些变化的可能性。)