(与C++0x,如何将元组扩展为可变模板函数参数?有关。)
以下代码(见下文)摘自此讨论。目标是将函数应用于元组。我简化了模板参数并修改了代码,以允许返回通用类型的返回值。
虽然原始代码可以编译通过,但当我尝试使用GCC 4.4.3编译修改后的代码时,
g++ -std=c++0x main.cc -o main
GCC报告了一个内部编译器错误(ICE),并显示以下消息:
main.cc: 在函数 ‘int main()’ 中:
main.cc:53: 内部编译器错误:在 cp/pt.c:10077 的 tsubst_copy 中
如果适当,请提交完整的错误报告和预处理源代码。
有关说明,请参见 <file:///usr/share/doc/gcc-4.4/README.Bugs>。
问题:代码是否正确?还是非法代码触发了内部编译器错误(ICE)?
// file: main.cc
#include <tuple>
// Recursive case
template<unsigned int N>
struct Apply_aux
{
template<typename F, typename T, typename... X>
static auto apply(F f, const T& t, X... x)
-> decltype(Apply_aux<N-1>::apply(f, t, std::get<N-1>(t), x...))
{
return Apply_aux<N-1>::apply(f, t, std::get<N-1>(t), x...);
}
};
// Terminal case
template<>
struct Apply_aux<0>
{
template<typename F, typename T, typename... X>
static auto apply(F f, const T&, X... x) -> decltype(f(x...))
{
return f(x...);
}
};
// Actual apply function
template<typename F, typename T>
auto apply(F f, const T& t)
-> decltype(Apply_aux<std::tuple_size<T>::value>::apply(f, t))
{
return Apply_aux<std::tuple_size<T>::value>::apply(f, t);
}
// Testing
#include <string>
#include <iostream>
int f(int p1, double p2, std::string p3)
{
std::cout << "int=" << p1 << ", double=" << p2 << ", string=" << p3 << std::endl;
return 1;
}
int g(int p1, std::string p2)
{
std::cout << "int=" << p1 << ", string=" << p2 << std::endl;
return 2;
}
int main()
{
std::tuple<int, double, char const*> tup(1, 2.0, "xxx");
std::cout << apply(f, tup) << std::endl;
std::cout << apply(g, std::make_tuple(4, "yyy")) << std::endl;
}
备注:如果我在递归情况下硬编码返回类型(参见代码),那么一切都很好。也就是说,用这段代码替换递归情况不会触发 ICE 错误:
// Recursive case (hardcoded return type)
template<unsigned int N>
struct Apply_aux
{
template<typename F, typename T, typename... X>
static int apply(F f, const T& t, X... x)
{
return Apply_aux<N-1>::apply(f, t, std::get<N-1>(t), x...);
}
};
哎呀,这只是原问题的一个不完整的解决方案。