我刚刚升级到GCC 4.8,一些可变参数模板代码不再正确编译。我在下面创建了一个最小的例子:
#include <tuple>
#include <iostream>
template <class T, class ... OtherT>
void something( std::tuple<T, OtherT...> & tup )
{
std::cout << std::get<1>(tup) << std::endl;
}
int main()
{
std::tuple<int, char, bool> myTuple(3, 'a', true);
// Compiles OK in GCC 4.6.3 but NOT 4.8
something<int, char, bool>( myTuple );
// Compiles OK in GCC 4.8 but NOT 4.6.3
something<int, bool, char>( myTuple );
return 0;
}
此输出将是(如果注释掉GCC 4.6.3 / 4.8的错误版本) 'A'。
GCC 4.6.3产生的错误是:
./test.cpp: In function ‘int main()’:
./test.cpp:18:39: error: no matching function for call to ‘something(std::tuple<int, char, bool>&)’
./test.cpp:18:39: note: candidate is:
./test.cpp:5:6: note: template<class T, class ... OtherT> void something(std::tuple<_Head, _Tail ...>&)
GCC 4.8产生的错误是:
./test.cpp: In function ‘int main()’:
./test.cpp:15:39: error: no matching function for call to ‘something(std::tuple<int, char, bool>&)’
something<int, char, bool>( myTuple );
^
./test.cpp:15:39: note: candidate is:
./test.cpp:5:6: note: template<class T, class ... OtherT> void something(std::tuple<_El0, _El ...>&)
void something( std::tuple<T, OtherT...> & tup )
^
./test.cpp:5:6: note: template argument deduction/substitution failed:
./test.cpp:15:39: note: mismatched types ‘bool’ and ‘char’
something<int, char, bool>( myTuple );
似乎在GCC 4.8中,可变参数模板类型在扩展时会被反转,但奇怪的是它们并没有像输出所证明的那样“真正地”反转 - 无论顺序如何,它都是'a'。 Clang 3.3同意GCC 4.6.3输出。
这是GCC 4.8中的错误还是其他什么?
编辑:在这里向GCC添加了一个错误报告:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56774
答案 0 :(得分:15)
对我来说这看起来像个错误,GCC 4.8.0和GCC 4.7.2似乎受到了影响。 Clang 3.2和GCC 4.6.3同意第一次调用something
是正确的,我真的没有看到GCC 4.7.2+如何认为第二次调用是可以接受的。
我会说:报告针对GCC的错误。
更新:我在GCC错误报告中添加了一个简约示例,只是为了帮助他们并证明它是一个纯粹的编译器错误,与std::tuple
无关。这是减少的代码:
template< typename... > struct X {};
template< typename T, typename... Ts >
void f( X< T, Ts... >& ) {}
int main()
{
X< int, bool, char > t;
f< int, char, bool >(t);
}
更新2:现在GCC 4.7.3,GCC 4.8.1和GCC 4.9已经修复 - 对GCC团队来说是一个非常快速的解决方案!