Resolve lambda init-capture pack grammar (CWG2378)

Document #: P2095R0
Date: 2020-02-14
Project: Programming Language C++
Audience: CWG
Reply-to: Barry Revzin
<>

1 Introduction

When [P0780R2] was adopted in Jacksonville in 2018, the paper contained a grammar rule that was inconsistent with other uses of packs. As pointed out by Richard Smith a few hours after plenary [Smith.Core], the grammar requires a pack of references to be captured like:

[...&x=init]

rather than how we write packs of references everywhere else.

[&...x=init]

This is now [CWG2378].

As of this writing, clang implements the Core Issue direction (with the & preceeding the ...) and gcc implements the standard wording. I opened a gcc bug for this [gcc.91847], but we should really fix the wording asap.

This paper exists to do that.

2 Wording

Change the grammar in 7.5.5.2 [expr.prim.lambda.capture]:

  capture:
-     simple-capture ...opt
-     ...opt init-capture
+     simple-capture
+     init-capture

  simple-capture:
-     identifier
-     & identifier
+     identifier ...opt
+     & identifier ...opt
      this
      * this

  init-capture:
-     identifier initializer
-     & identifier initializer
+     ...opt identifier initializer
+     & ...opt identifier initializer

Change 7.5.5.2 [expr.prim.lambda.capture]/2:

If a lambda-capture includes a capture-default that is =, each simple-capture of that lambda-capture shall be of the form “& ...opt identifier”, “this”, or “* this”.

Change 7.5.5.2 [expr.prim.lambda.capture]/6:

An init-capture without ellipsis behaves as if it declares and explicitly captures a variable of the form “auto init-capture ;” whose declarative region is the lambda-expression’s compound-statement, except that:

Change 7.5.5.2 [expr.prim.lambda.capture]/17:

17 A simple-capture followed by containing an ellipsis is a pack expansion ([temp.variadic]). An init-capture preceded by containing an ellipsis is a pack expansion that introduces an init-capture pack ([temp.variadic]) whose declarative region is the lambda-expression’s compound-statement.

Change 13.7.3 [temp.variadic]/5.10:

5 […] Pack expansions can occur in the following contexts:

  • (5.10) In a capture-list ([expr.prim.lambda]); the pattern is a capture the capture without the ellipses.

3 References

[CWG2378] Barry Revzin. 2018. Inconsistent grammar for reference init-capture of pack.
https://wg21.link/cwg2378

[gcc.91847] Barry Revzin. 2019. init-capture pack of references requires … on wrong side.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91847

[P0780R2] Barry Revzin. 2018. Allow pack expansion in lambda init-capture.
https://wg21.link/p0780r2

[Smith.Core] Richard Smith. 2018. p0780r2 has wrong grammar for reference init-capture packs.
https://lists.isocpp.org/core/2018/03/4095.php