模板参数无效

时间:2014-02-01 21:59:51

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

我正在努力学习模板!请考虑以下代码:

template <typename... Ts> struct Sequence {};

template<unsigned N> struct B {
  template<unsigned P> struct C {
    typedef int type;
  };
};

然后这是完全正确的:

template<unsigned... Is>
struct Loop2 {
  typedef Sequence< typename B<5>::C<Is>::type... > type;
};

Loop2<3,1> l;

因此我无法理解为什么这个模板版本:

template<unsigned N, unsigned... Is>
struct Loop3 {
  typedef Sequence< typename B<N>::C<Is>::type... > type;
};
编译器不接受

。它引发以下错误:

essai.cpp:29:51: error: template argument 1 is invalid
   typedef Sequence< typename B<N>::C<Is>::type... > type;

我得到的信息

g++ (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]

感谢您的帮助!

顺便说一下:欢迎提出更好的头衔!

1 个答案:

答案 0 :(得分:4)

由于B不再是特定类型,您需要使用template关键字标记C,因为它取决于N的值。使用以下代码应该有效:

template<unsigned N, unsigned... Is>
struct Loop3 {
  typedef Sequence< typename B<N>::template C<Is>::type... > type;
};