Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC 7 C++ 17 support for folding expressions

Tags:

c++

gcc

c++17

gcc7

The following snippet will compile in GCC 8+, but fails to compile in GCC 7.

template <typename... THINGS>
struct A
{
  explicit A(THINGS *... things)
  {
    (..., [thing = things](){}());
  }
};


int main()
{
    int thing;
    const auto thingy = A{&thing};
}

godbolt

The stated failure is that the parameter pack isn't being expanded: parameter packs not expanded with '...'.
Checking the GCC standards compliance page, fold expressions should be supported in GCC 7.

Is there another flag i need besides std=c++17? (i didn't see one)
Is the standard not yet completely implemented? (i didn't see anything indicating that)
Can i make this work, or is this just a GCC 7 bug i'm going to have to work around?

like image 209
Taekahn Avatar asked Dec 22 '22 20:12

Taekahn


1 Answers

This is a GCC bug, originally reported in version 8.01, fixed in version 8.2. It seems that the bug also occurs when fold-expressions are not used (the C++11-era "expander trick" mentioned by NathanOliver doesn't work either), so you'll have to use a longer workaround that doesn't require expanding a template parameter pack that is inside a lambda capture. For example:

template <typename THING>
void do_it(THING* thing) {
    [thing]{}();
}

explicit A(THINGS *... things)
{
    (..., do_it(things));
}
like image 50
Brian Bi Avatar answered Dec 25 '22 23:12

Brian Bi