C++ GCC 7 C++;17对折叠表达式的支持
以下代码段将在GCC 8+中编译,但在GCC 7中编译失败C++ GCC 7 C++;17对折叠表达式的支持,c++,gcc,c++17,gcc7,C++,Gcc,C++17,Gcc7,以下代码段将在GCC 8+中编译,但在GCC 7中编译失败 template <typename... THINGS> struct A { explicit A(THINGS *... things) { (..., [thing = things](){}()); } }; int main() { int thing; const auto thingy = A{&thing}; } 模板 结构A { 显式A(事物*…事物) {
template <typename... THINGS>
struct A
{
explicit A(THINGS *... things)
{
(..., [thing = things](){}());
}
};
int main()
{
int thing;
const auto thingy = A{&thing};
}
模板
结构A
{
显式A(事物*…事物)
{
(…,[thing=things]({}());
}
};
int main()
{
智力事物;
const auto thingy=A{&thing};
}
所述故障是参数包未展开:参数包未展开为“…”
检查,折叠表达式应在GCC 7中得到支持
template <typename... THINGS>
struct A
{
explicit A(THINGS *... things)
{
(..., [thing = things](){}());
}
};
int main()
{
int thing;
const auto thingy = A{&thing};
}
除了std=c++17
,我还需要另一个标志吗?(我没有看到一个)该标准是否尚未完全实施?(我没有看到任何迹象表明这一点)
我能做到这一点吗,或者这只是一个GCC 7缺陷,我需要解决吗?这是一个GCC缺陷。发现问题,已在8.2中修复。这是一个问题,最初在8.01版中报告,在8.2版中修复。当不使用折叠表达式时,该错误似乎也会发生(NathanOliver提到的C++11时代的“扩展器技巧”也不起作用),因此您必须使用更长的解决方法,不需要扩展lambda捕获中的模板参数包。例如:
template <typename THING>
void do_it(THING* thing) {
[thing]{}();
}
explicit A(THINGS *... things)
{
(..., do_it(things));
}
模板
空做它(事*事){
[事物]{}();
}
显式A(事物*…事物)
{
(…,做事);
}
FWIW您应该能够使用数组技巧:intdiscard[]={([thing=things](){}(),0)…}代码>@NathanOliver但由于相同的GCC错误,这会产生相同的错误?@Oh。我认为问题在于折叠表达式本身,而不是lambda捕获破坏了表达式。现在我已经阅读了这个bug,我发现它确实不起作用。