14

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?

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 1
    FYI, GCC has some trouble with complex pack expansion. [this](https://stackoverflow.com/questions/49491838/creating-a-variant-type-of-all-possible-map-of-key-value-types-at-compile-time/49492624#49492624) – llllllllll Apr 08 '18 at 21:37
  • 6
    This is [47226](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226) – Barry Apr 08 '18 at 21:44
  • @Barry: I'm done. I'll just bookmark that and make a mental note to open it before posting a question – Vittorio Romeo Apr 08 '18 at 21:55
  • @Barry: wait - it says that it has been fixed... yet it still happens on trunk – Vittorio Romeo Apr 08 '18 at 21:57

1 Answers1

9

This has the look of a bug about it.

[temp.variadic]/4

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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458