【问题标题】:Derive from class template which is nested in class template, all withing a class template [duplicate]从嵌套在类模板中的类模板派生,全部在类模板中[重复]
【发布时间】:2019-11-23 13:43:58
【问题描述】:

我正在努力让它发挥作用:

template<template<typename> class... Attributes>
struct Skills
{
    template<class DERIVED>
    struct Create : public Attributes<DERIVED>...
    {};
};

template <class T, class SKILLS>
class Tester : public typename SKILLS::Create<Tester<T,SKILLS>>
{}

但编译器抱怨:

错误 C2518:基类列表中的关键字 'typename' 非法;忽略

但是,如果我不在类模板中派生,这可以正常工作。

有没有机会从嵌套模板类派生?

我为什么需要它?我正在尝试为 CRTP 类模板实现一个更好的接口,它允许执行以下操作:

using MyClass = ClassWithAttributes<Skills<Plus,Minus,Equal,Clear,Size>>

【问题讨论】:

    标签: c++ templates template-meta-programming crtp


    【解决方案1】:

    你必须写

    template <class T, class SKILLS>
    class Tester : public SKILLS::template Create<Tester<T,SKILLS>>
    { };
    

    我是说

    (1) 否typename:在基类列表中隐含参数是类型

    (2) 你需要template,在Create之前

    (3) 使用SKILLS 作为Tester 的参数,而不是Skills,因为你已经声明了模板参数SKILLS

    (4) 记住类定义末尾的分号

    【讨论】:

    • 谢谢,就是这样。 (3) 只是一个错字,我编辑了这个。
    猜你喜欢
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2020-12-16
    • 2016-05-07
    • 2017-01-13
    • 1970-01-01
    • 2016-02-17
    • 2015-04-10
    相关资源
    最近更新 更多