成员类型模板专业化

Member Type Template Specialization

我有一个 class 模板 S1,带有两个整数参数:

template <int A, int B> struct S1 { };

我有另一个带有一个整数参数的 class 模板 S2。在 S2 中有一个专门用于 S1 的类型模板 My_S1。当 S1 通过 main:

中的 S2 的特化实例化时,它工作正常
template<int A> struct S2 {
  template<int B>
  using My_S1 = S1<A, B>;
};

int main(int argc, char *argv[]) {
  using S1_a = S2<0>::My_S1<0>;
}

但是如果我尝试在下面的第三个 class 模板中执行此操作,我会在编译时遇到错误。完整代码:

template<int A, int B>
struct S1 { } ;

template<int A>
struct S2 {
  template<int B>
  using My_S1 = S1<A, B>;
};

template<int A, int B>
struct S3 {
  using My_S1 = typename S2<A>::My_S1<B>; // I get an error on this line
};

int main(int argc, char * argv[]) {
  using S1_a = S1<0, 0>;         // works fine, of course
  using S1_b = S2<0>::My_S1<0>;  // works fine
  using S1_c = S3<2,1>::My_S1;   // ?
  return 0;
}

我得到的错误如下:

error: expected ‘;’ before ‘<’ token
using My_S1 = typename S2<A>::My_S1<B>;
                                   ^
error: expected unqualified-id before ‘<’ token

你必须明确地告诉编译器 S2<A>::My_S1 是一个模板:

//                            vvvvvvvv-- here
using My_S1 = typename S2<A>::template My_S1<B>;