7
votes

gcc's treatment of variadic templates is well known to be patchy (see for example this and this), but I wonder whether the following bug is already known (I cannot find it at bugzilla) or whether it is indeed a bug. Essentially, gcc (4.8.1) fails to expand a parameter pack inside a lambda:

#include <vector>
#include <algorithm>
#include <type_traits>

template<typename T, typename F, typename... X>
void bar(std::vector<T> const&c, F const&f, X&&... x)
{
  std:for_each(c.begin(),c.end(),[&](const T&t)
         { f(t,std::forward<X>(x)...); });
}

this causes (even without any instantiation)

error: parameter packs not expanded with ‘...’:
  { f(t,std::forward<X>(x)...); });
                        ^

any idea how to avoid that? (note: okay with icpc 14.0.2 and clang 3.4) Or is gcc correct after all and clang & icpc wrong?

edit Note that the problem is the lambda, as also this doesn't compile:

template<typename T, typename F, typename... X>
void bar(std::vector<T> const&c, F const&f, X&&... x)
{
auto func = [&](const T&t){ f(t,std::forward<X>(x)...); };
std:for_each(c.begin(),c.end(),func);
}

with the "error" report in the lambda defiition.

1
What is the first parameter passed t? - Manu343726
@Manu343726 What does it matter? The error reports even with no instantiation. See it live - WhozCraig
Random suggestion: [&x...] do anything? Or stuff them in a tuple<X&&...> before pulling them out with an std::get<Is>(tup)... - Yakk - Adam Nevraumont
@Yakk Why should [&x...] help? I could try the tuple trick, but I'm afraid the compiler will not optimise that away ... - Walter
@walter Just stabs at workarounds: the compiler is wrong. - Yakk - Adam Nevraumont

1 Answers

8
votes

Given that the code compiles cleanly with both clang version 3.5 (trunk 202594) and more importantly with gcc version 4.9.0 20140302 (experimental) (GCC), both with -Wall, I would say it was an issue with the earlier versions of gcc.

I am looking for a gcc bugreport at http://gcc.gnu.org/bugzilla/ to confirm this.