【发布时间】:2014-04-29 09:41:22
【问题描述】:
众所周知,gcc 对可变参数模板的处理是不完整的(参见例如 this 和 this),但我想知道以下错误是否已知(我在 bugzilla 找不到)或者是否确实一个错误。本质上,gcc (4.8.1) 无法在 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)...); });
}
这会导致(即使没有任何实例化)
error: parameter packs not expanded with ‘...’:
{ f(t,std::forward<X>(x)...); });
^
知道如何避免这种情况吗? (注意:icpc 14.0.2 和 clang 3.4 没问题)还是 gcc 是正确的,而 clang 和 icpc 是错误的?
edit 请注意,问题在于 lambda,因为它也无法编译:
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);
}
在 lambda 定义中带有“错误”报告。
【问题讨论】:
-
t传递的第一个参数是什么? -
@Manu343726 这有什么关系?即使没有实例化也会报告错误。 See it live
-
随机建议:
[&x...]做什么?或者把它们塞进tuple<X&&...>,然后再用std::get<Is>(tup)...取出来 -
@Yakk 为什么要
[&x...]提供帮助?我可以尝试元组技巧,但恐怕编译器不会优化它... -
@walter 只是尝试解决方法:编译器错误。
标签: c++ gcc c++11 lambda variadic-templates