简体   繁体   English

使用继承的类模板

[英]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: 注意事项:

  • Is there a way to refer to 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. 基本上这就是为什么选项#3不起作用以及为什么我在选项#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. 当引用专门化时,它也将完全代表从基类成员模板获得的相同类型。

See it live 现场观看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM