c++gccc++17gcc7

GCC 7 C++ 17 support for folding expressions


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?


Solution

  • 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));
    }