gpt4 book ai didi

c++ - 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断

转载 作者:IT老高 更新时间:2023-10-28 21:42:01 26 4
gpt4 key购买 nike

关于 lambda 的部分捕获 ([expr.prim.lambda.capture]/5 ) 声明

If an identifier in a simple-capture appears as the declarator-id of a parameter of the lambda-declarator's parameter-declaration-clause, the program is ill-formed.

考虑以下示例:

#include <iostream>

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

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

这些编译器是否应该有诊断(错误/警告)(如前所述在引用)沿线:

// parameter and simple-capture have the same name

Godbolt 演示 here

最佳答案

该措辞已添加到 C++17 以解决 CWG Defect 2211 .它在 C++14 中不存在,而且 Clang 和 GCC 似乎还没有 catch 你正在检查的版本的这种变化。

值得注意的是,GCC 主干确实将该程序诊断为 C++17 下的格式错误。

关于c++ - 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com