Compiler bug, or non standard code? - Variadic template capture in lambda

[亡魂溺海] 提交于 2019-12-10 14:51:53

问题


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);
}

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

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();
}

— end example ]

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...); };
                   ^

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


回答1:


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:

sorry, unimplemented: use of ‘type_pack_expansion’ in template

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



来源:https://stackoverflow.com/questions/18871122/compiler-bug-or-non-standard-code-variadic-template-capture-in-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!