GCC 4.8正在逆转可变模板参数包

GCC 4.8 is reversing variadic template parameter pack

本文关键字:参数 逆转 GCC      更新时间:2023-10-16

我刚刚升级到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中的一个bug还是别的什么?

编辑:在这里向GCC添加了一个bug报告:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56774

这看起来像是一个bug, GCC 4.8.0和GCC 4.7.2似乎受到了影响。Clang 3.2和GCC 4.6.3都同意第一次调用something是正确的,我真的不明白GCC 4.7.2+怎么能认为第二次调用是可以接受的。

我会说:报告一个针对GCC的bug。


更新:我在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团队的快速修复!