模板成员类的模板别名
Template alias for template member class
我有以下模板
template<class F>
struct A{
template<int N>
struct B{
using type = int;
};
};
不过,我想制作一个模板别名:
//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
调试时我发现:
//does compile
struct C{};
using alias_B = typename A<C>::B<0>::type;
有人能指出我做错了什么吗?我觉得我错过了一些显而易见的东西。
您需要告诉C++它的内部类型B<N>
是一个模板:
template<class F, int N >
using alias_A = typename A<F>::template B<N>::type;
在这种情况下,编译器将您编写的内容解析为operator<
,而不是模板参数的大括号。
这篇文章详尽地介绍了你何时以及为什么需要这样做。
相关文章:
- 为什么在C++中首先初始化成员类
- 非虚拟基的多态成员类
- 当从成员类调用封装的std::begin时,程序崩溃
- 从私有成员类中断封装派生的模板类
- 异常成员类 OOP
- 如何从外部类 (c++) 构造成员类对象
- 调用虚拟成员类的方法
- C++11 成员类初始化顺序
- 成员类函数
- 用ifstream构造一个类诗人,然后用相同的ifstream构造它的成员类诗句
- 从成员类功能创建线程
- 如何在c++中实现内部抽象成员类
- 初始化静态成员类(用于信号处理程序)
- C++ 定义不带模板参数的成员类的类型
- 模板类中的不可复制静态 const 成员类
- 成员类是否可以从其父类继承?
- 成员类之间的通信的正确方式是什么?
- 对成员类/对象变量使用指针更好吗
- 模板成员类的模板别名
- C++:它是如何工作的别名成员类