Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding parameter pack as part of lambda capture in fold expression - gcc vs clang [duplicate]

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?

like image 624
Vittorio Romeo Avatar asked Apr 08 '18 21:04

Vittorio Romeo


1 Answers

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.

like image 96
StoryTeller - Unslander Monica Avatar answered Oct 14 '22 03:10

StoryTeller - Unslander Monica