[英]Using a inherited class template
In the following excerpt, how can I access the inherited Sub
class template? 在以下摘录中,如何访问继承的
Sub
类模板?
As far as I understand the problem with the constellation below is that the base class Base
is itself a dependent class. 据我所理解,下面这个星座的问题是基类
Base
本身是一个依赖类。 Accessing it using typename
/ template
works but is cumbersome if Sub
is needed frequently. 使用
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 caveats: 注意事项:
Sub
without having to duplicate the specialization for Base
? Sub
而不必重复Base
的专门化? Ie, consider Base
having numerous/complex template arguments. Base
。 This is basically why option #3 does not work and why I chose Class
as qualification in option #4. Class
作为资格的原因。 If you need the template Sub
, then you can go with option 5, an alias template: 如果需要模板
Sub
,则可以使用选项5,即别名模板:
template<int I>
using Sub = typename Base<C>::template Sub<I>;
Now Sub
is found inside Class
during unqualified name lookup, and is known to be a template. 现在,在非限定名称查找期间,可以在
Class
内找到Sub
,并且知道这是一个模板。 It will also stand exactly for the same type obtained from the base class member template, when a specialization is referred to. 当引用专门化时,它也将完全代表从基类成员模板获得的相同类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.