在以下摘录中,如何访问继承的Sub
类模板?
据我所知,下面的星座问题是基类Base
本身就是一个依赖类。使用typename
/ template
访问它有效但如果经常需要Sub
则很麻烦。
template<int B>
struct Base {
template<int S>
class Sub { };
};
template<int C>
struct Class: public Base<C> {
// (1) Error: 'Sub' does not name a type
using S2 = Sub<2>;
// (2) Error: 'Base' used without template argument list
using S3 = Base::Sub<3>;
// (3) Error: 'Class' is incomplete here
using S4 = Class::Sub<4>
// (4) Works, but complicated
using S1 = typename Class::template Sub<1>;
};
using Class0 = Class<0>;
int main() { }
Fruther警告:
Sub
来复制Base
的专业化?即,考虑Base
具有多个/复杂的模板参数。这基本上是为什么选项#3不起作用以及为什么我选择Class
作为选项#4中的资格。答案 0 :(得分:0)
如果您需要模板 Sub
,那么您可以使用选项5,别名模板:
template<int I>
using Sub = typename Base<C>::template Sub<I>;
现在Sub
在非限定名称查找期间在Class
内找到,并且已知是模板。当引用特化时,它也将完全适用于从基类成员模板获得的相同类型。