Expanding parameter pack as part of lambda capture in fold expression – gcc vs clang

  • A+

Consider the following code snippet:

template <int... Is> void foo() {     ([i = Is]{}(), ...);  } 
  • clang++ (trunk) successfully compiles the code with -std=c++17

  • g++ (trunk) fails to compile with the following error:

    <source>: In function 'void foo()':  <source>:4:11: error: parameter packs not expanded with '...':     ([i = Is]{}(), ...);         ^~  <source>:4:11: note:         'Is' <source>:4:16: error: operand of fold expression has no unexpanded parameter packs     ([i = Is]{}(), ...);     ~~~~~~~~~~^~ 

    on godbolt.org

Is this a g++ bug, or does the Standard prevent expansion of a parameter pack as part of a lambda-introducer?

This has the look of a bug about it.


A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list (described below). The form of the pattern depends on the context in which the expansion occurs. Pack expansions can occur in the following contexts:

  • ... [not relevant]
  • In a fold-expression; the pattern is the cast-expression that contains an unexpanded parameter pack.

A complete lambda expression (like you have) with a function call, is a valid cast-expression if one follows the grammar productions. There is no reason to preclude it from being a valid pattern.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: