Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top