GCC 4.8.1的变通方法:抱歉,未实现:弄乱参数\u pack\u select

GCC 4.8.1的变通方法:抱歉,未实现:弄乱参数\u pack\u select,gcc,c++11,compiler-errors,variadic-templates,compiler-bug,Gcc,C++11,Compiler Errors,Variadic Templates,Compiler Bug,考虑以下代码: #include <tuple> template <class Result, class Function, class... Types> Result f(Function func, Types... values) { return std::get<0>(std::make_tuple(func(values)...)); } template <class... Types> int g(const Ty

考虑以下代码:

#include <tuple>

template <class Result, class Function, class... Types>
Result f(Function func, Types... values)
{
    return std::get<0>(std::make_tuple(func(values)...));
}

template <class... Types>
int g(const Types... values)
{
    return std::get<0>(std::make_tuple(f<Types>([](int n){return n;}, values)...));
}

int main()
{
    return g(42);
}
#包括
模板
结果f(函数函数、类型…值)
{
返回std::get(std::make_tuple(func(value)…);
}
模板
int g(常量类型…值)
{
return std::get(std::make_tuple(f([](int n){return n;},value)…);
}
int main()
{
返回g(42);
}
在g++4.8.1下,它生成:

mangling.cpp: In instantiation of ‘g(const Types ...) [with Types = int]::__lambda0’:
mangling.cpp:12:50:   required from ‘struct g(const Types ...) [with Types = int]::__lambda0’
mangling.cpp:12:77:   required from ‘int g(const Types ...) [with Types = int]’
mangling.cpp:17:16:   required from here
mangling.cpp:12:57: sorry, unimplemented: mangling argument_pack_select
     return std::get<0>(std::make_tuple(f<Types>([](int n){return n;}, values)...));
                                                         ^
mangling.cpp: In instantiation of ‘struct g(const Types ...) [with Types = int]::__lambda0’:
mangling.cpp:12:77:   required from ‘int g(const Types ...) [with Types = int]’
mangling.cpp:17:16:   required from here
mangling.cpp:12:57: sorry, unimplemented: mangling argument_pack_select
mangling.cpp:12:57: sorry, unimplemented: mangling argument_pack_select
mangling.cpp:4:8: error: ‘Result f(Function, Types ...) [with Result = int; Function = g(const Types ...) [with Types = {int}]::__lambda0; Types = {int}]’, declared using local type ‘g(const Types ...) [with Types = {int}]::__lambda0’, is used but never defined [-fpermissive]
 Result f(Function func, Types... values)
mangling.cpp:g(const-Types…[with-Types=int]:\uu lambda0]的实例化中:
mangling.cpp:12:50:从“结构g(常量类型…[类型=int]:\uu lambda0”中需要
mangling.cpp:12:77:必须来自“int g(const类型…[类型=int]”
mangling.cpp:17:16:从这里开始需要
mangling.cpp:12:57:抱歉,未实现:mangling参数\u pack\u select
return std::get(std::make_tuple(f([](int n){return n;},value)…);
^
mangling.cpp:在“struct g(const Types…[with Types=int]::”的实例化中:
mangling.cpp:12:77:必须来自“int g(const类型…[类型=int]”
mangling.cpp:17:16:从这里开始需要
mangling.cpp:12:57:抱歉,未实现:mangling参数\u pack\u select
mangling.cpp:12:57:抱歉,未实现:mangling参数\u pack\u select
mangling.cpp:4:8:error:'Result f(Function,Types…[with Result=int;Function=g(const Types…[with Types={int}]:::::{u lambda0;Types={int}]'使用局部类型'g(const Types…[with Types={int}]::}声明,但从未定义[-fpermissive]
结果f(函数函数、类型…值)
是否有避免此问题的解决方法?是否在g++4.8.2或4.9.0中报告并更正了该问题


编辑:我刚刚在这里报告了错误:

您是否需要为每个扩展参数添加新的lambda?否则,这将修复它:

template <class... Types>
int g(const Types... values)
{
    auto func = [](int n){return n;};
    return std::get<0>(std::make_tuple(f<Types>(func, values)...));
}
模板
int g(常量类型…值)
{
auto func=[](int n){return n;};
返回std::get(std::make_tuple(f(func,value)…);
}

在gcc 4.9中给出了一个ICE。文森特:发布一个您试图解决的实际问题的示例@丹尼尔弗里的回答是正确的:如果你只是想避开冰,你所要做的就是把lambda拉到包扩展之外。(如果您需要
constexpr
,解决方案是一样的。我可以发布代码,但在您真正告诉我们您的所有秘密要求之前,我不想偏离Daniel的答案。)@Quuxplusone我正在处理一个严重依赖最糟糕/优雅的元编程技巧的代码,我已经在本例中隔离了问题。但基本上,我需要做与所提供示例中引起问题的完全相同的事情(这不是XY问题)(翻译:是的,你正在处理一个XY问题,在你告诉我们X之前,我们唯一能做的就是为你解Y,这显然对你不是很有用。)@Vincent不确定C++1y是否是你的一个选项,以及它是否真的适用于
constexpr
,但有帮助吗?@Daniel In C++1y,您可以将
constexpr
添加到您在答案中编写的函数中!无论如何,在C++11中,您可以通过适当使用helper函数来表示任何内容。也就是说,您将
g
实现为
返回ghelper([](int n){return n;},value其中
ghelper
以明显的方式实现。