编译器错误,还是非标准代码? - 在lambda中的可变模板捕获 [英] Compiler bug, or non standard code? - Variadic template capture in lambda

查看:730
本文介绍了编译器错误,还是非标准代码? - 在lambda中的可变模板捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C ++ 11代码;

I have the following C++11 code;

template<typename... T>
int g(T... t)
{
    return 0;
}

template<class... Args>
void f(Args... args)
{
    auto lm = [&, args...] { return g(args...); };
    lm();
}

int main()
{
    f(2, 5, 7);
}



我相信它是有效的C ++ 11,根据;

I do believe that it's valid C++11, according to; Section 5.1.2.23 of the standard;


捕获后跟一个省略号是一个
pack扩展(14.5.3)。 [示例:

A capture followed by an ellipsis is a pack expansion (14.5.3). [ Example:

template<class... Args> void f(Args... args) {
    auto lm = [&, args...] { return g(args...); }; lm();
}

- 结束示例]

但是当Clang ++编译得很好时,G ++提供了这个错误;

However while Clang++ compiles fine, G++ provides this error;

main.cpp: In function 'void f(Args ...)':
main.cpp:10:23: error: expected ',' before '...' token
     auto lm = [&, args...] { return g(args...); };
                   ^
main.cpp:10:23: error: expected identifier before '...' token
main.cpp:10:26: error: parameter packs not expanded with '...':
     auto lm = [&, args...] { return g(args...); };
                      ^
main.cpp:10:26: note:         'args'
main.cpp: In lambda function:
main.cpp:10:43: error: expansion pattern 'args' contains no argument packs
     auto lm = [&, args...] { return g(args...); };
                                       ^
main.cpp: In instantiation of 'struct f(Args ...) [with Args = {int, int, int}]::__lambda0':
main.cpp:10:49:   required from 'void f(Args ...) [with Args = {int, int, int}]'
main.cpp:16:14:   required from here
main.cpp:10:19: error: using invalid field 'f(Args ...)::__lambda0::__args'
     auto lm = [&, args...] { return g(args...); };
                   ^

所以我的问题是,这是G ++中的编译器错误吗? p>

So my question is simply, is this a compiler bug in G++?

推荐答案

看起来支持在GCC中尚未实现。反之亦然,你不能有一个lambda在一个pack扩展(每个包参数产生一个lambda)。

It looks like support has not been implemented in GCC. Vice versa, you cannot have a lambda inside a pack expansion (to produce one lambda per pack argument). It seems the two features don't play together at all.

如果你只是简单地使用 [&] ,则会出现更有帮助的错误消息:

If you simply use [&] instead, then there is a more helpful error message:


抱歉,未实现:在模板中使用'type_pack_expansion'

sorry, unimplemented: use of ‘type_pack_expansion’ in template

免责声明:我的GCC副本是在7月下旬建成的;我可能需要升级。

Disclaimer: My copy of GCC was built in late July; I'm probably due for an upgrade.

这篇关于编译器错误,还是非标准代码? - 在lambda中的可变模板捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆