C++23中省略参数列表的lambda表达式的有效性

根据cppreference,gcc 和 clang 最近都完成了P1102R2(“ Down with ()!”)的实现,这意味着我们可以在 C++23 中更简洁地定义 lambda 表达式。

但是我发现它们与某种形式不一致:

auto l = []<auto> noexcept requires true {};

clang 接受这种形式,而 gcc拒绝其语法。

我应该信任哪个编译器?这个 lambda 在 C++23 中是格式正确还是格式错误?

更新:

或许是迫于舆论压力,clang在我举报后的五天内迅速修复了49736。

再进一步尝试时,无意中发现gcc也拒绝了以下有效表单,这让我报告了99850,2周后修复。

auto l = []<auto> requires true -> void {};

回答

感谢您提醒我此功能是多么无意义。

正确答案是:不,这不是一个格式良好的 lambda。语法在[expr.prim.lambda.general] 中定义:

在我们的例子中,首先我们有:

[]<auto> noexcept requires true {};
  • []lambda 引入者
  • <auto>匹配<template-parameter-list>,现在我们知道我们是第二种lambda 表达式。所以在语法上,我们需要跟在requires 子句(可选)然后是lambda-declarator然后是复合语句
  • noexcept不匹配requires-clause,所以现在我们正在解析lambda-declarator。一个lambda-declarator可以开始,(parameter-declaration-clause)但我们没有,所以我们只是在寻找lambda-specifiers。我们将noexcept用作noexcept-specifier 的一部分。
  • requires true不适合attribute-specifier-seqtrailing-return-type所以我们都没有,现在我们完成了lambda-specifiers所以我们完成了lambda-declarator。此时,我们正在寻找复合语句。但是我们没有那个,所以这是一个错误。

基本上,有两个地方可以放置一个requires 子句:直接在模板参数之后,或者,如果我们有函数参数,则在函数参数之后的lambda 说明符之后。所以这有效:

[]<auto> requires true noexcept {};

就像这样:

[]<auto>() noexcept requires true {};

就像这样:

[]<auto> requires true () noexcept requires true { };

但不是 OP 中的那个。

另外,不要写这个。

  • The observation that led to the current rule is that the requires-clause is either constraining the explicit template parameter list or the explicit function parameter list. As such, an explicit `<>` list can be followed by a requires-clause, and an explicit `()` list can be followed by a requires-clause. If you omit the explicit list, you can't include a requires clause for it.
  • I filed a [bug report](https://bugs.llvm.org/show_bug.cgi?id=49736) for llvm this morning, but I am not 100% sure that it should be ill-formed. Thanks for letting me know I'm right.
  • @Deduplicator: It was simply not considered worthwhile to make a complicated grammar to make a non-empty *lambda-specifiers* differentiate the two potential *requires-clause*s.

以上是C++23中省略参数列表的lambda表达式的有效性的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>