c ++ variadic模板,错误的参数包长度?

时间:2012-05-10 12:35:37

标签: c++ c++11 variadic-templates

这是一个gcc错误还是以某种方式错误地使用参数包?

(用gcc 4.6.3编译:)

#include <iostream>
template<class...Ts> struct tuple{};
template<class...>class test;

template< template <class...> class tp,
  class...arg1Ts,
  class...arg2Ts> 
class test<tp<arg1Ts...>,tp<arg2Ts...>>{
  public:
  void test1(arg1Ts... arg1s,arg2Ts... arg2s){
  std::cout<<sizeof...(arg1s);  //Why is this 2? Why not 0?
  std::cout<<sizeof...(arg2s);  //2 ok
 }
};
int main(){
 test<tuple<>,tuple<char,int>> t1; //(arg1Ts... = empty), (arg2Ts... = char,int)
 t1.test1('a',2); //prints 22, not 02
}

1 个答案:

答案 0 :(得分:2)

我在这里写了一个可编辑的例子:

#include <iostream>

template <class...> class test;

template <
    template <class...> class tp,
    class...arg1Ts,
    class...arg2Ts
> 
class test < tp<arg1Ts...>, tp<arg2Ts...> > {
public:
    void test1(arg1Ts..., arg2Ts...) {
        std::cout << sizeof...(arg1Ts) << ' ' << sizeof...(arg2Ts) << '\n';
    }
};

template <typename...> class Pack;

int main(){
   test< Pack<>, Pack<char,int>> t1;
   t1.test1('a', 2);
}

(与你的没什么不同,但它干净利落地编译)

使用clang 3.0,这给出了:

0 2

这正是预期的结果。所以我想你正在使用的gcc版本有一个bug。是时候进入4.7?