当simple-capture中的标识符显示为参数的declarator-id时,没有编译器诊断

P.W*_*P.W 22 c++ lambda language-lawyer

关于lambda捕获的部分([expr.prim.lambda.capture]/5)表明了这一点

如果simple-capture中的标识符显示为lambda-declarator的parameter-declaration-clause参数的declarator-id,则该程序格式错误.

考虑以下示例:

#include <iostream>

int main ()
{
    auto foo = 1234;
    auto bar = [foo](int foo) { std::cout << foo << '\n'; };
    bar(4321);     
}
Run Code Online (Sandbox Code Playgroud)

最新的GCC版本(8.2.0 - 2018年7月26日发布)没有对此进行诊断.最新的Clang版本(7.0.0 - 2018年9月19日发布)也没有.

是否应该从这些编译器(如参考文献中提到)进行诊断(错误/警告),方法如下:

// parameter and simple-capture have the same name
Run Code Online (Sandbox Code Playgroud)

Godbolt Demo 在这里

Sto*_*ica 22

该措辞被添加到C++ 17以解决CWG缺陷2211.它在C++ 14中不存在,看起来Clang和GCC还没有赶上你正在检查的版本.

值得注意的是,GCC主干确实诊断出该程序在C++ 17下是不正确的.