模板成员类的模板别名

Pol*_*mer 1 c++ c++11

我有以下模板

template<class F>
struct A{
  template<int N>
  struct B{
    using type = int;
  };
};
Run Code Online (Sandbox Code Playgroud)

但是,我想创建一个模板别名:

//doesn't compile.
template<class F, int N >
using alias_A = typename A<F>::B<N>::type;

GCC:
question.cpp:12:36: error: expected ';' before '::' token
 using alias_A = typename A<F>::B<N>::type;
                                    ^
question.cpp:12:36: error: 'type' in namespace '::' does not name a type
Run Code Online (Sandbox Code Playgroud)

调试时我发现:

//does compile
struct C{};
using alias_B = typename A<C>::B<0>::type;
Run Code Online (Sandbox Code Playgroud)

有人可以指出我做错了什么吗?我觉得我缺少明显的东西。

Yuu*_*shi 5

您需要告诉C ++,它的内部类型B<N>是模板:

template<class F, int N >
using alias_A = typename A<F>::template B<N>::type;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,编译器将解析您编写的内容operator<,而不是将其作为模板参数的大括号。

这篇文章详尽介绍了何时以及为什么需要这样做。