【问题标题】:How can a template class inherit from a nested template class模板类如何继承嵌套的模板类
【发布时间】:2016-10-22 14:21:16
【问题描述】:

考虑以下代码:

template<class C, class P>
//class Cchild : public C::NestedClass              // Works
class Cchild : public C::NestedTemplateClass<P>     // Fails : how to inherit from nested template class defined in C ?
{
};

当模板类 Cchild 继承自嵌套类 C::NestedClass 时,编译器可以正常编译。但是,如果我希望 Cchild 从嵌套模板类 C::NestedTemplateClass&lt;P&gt; 继承,则编译失败

我该怎么做?

【问题讨论】:

    标签: c++ templates nested


    【解决方案1】:

    语法是:

    template<class C, class P>
    class Cchild : public C::template NestedTemplateClass<P>
    {
    };
    

    来自 gcc 的错误信息实际上非常易读:

    t.C:3:26: error: non-template ‘NestedTemplateClass’ used as template
     class Cchild : public C::NestedTemplateClass<P>
                              ^~~~~~~~~~~~~~~~~~~
    t.C:3:26: note: use ‘C::template NestedTemplateClass’ to indicate that it is a template
    

    【讨论】:

    • 我虽然尝试过这种语法,但显然没有。是的,如果我使用gcc 而不是vc,我可能会在错误消息中得到答案。谢谢山姆。
    猜你喜欢
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多