Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板专门化继承自特定的子类 类基类{…}; 类Derived1:Base{…}; 类Derived2:Base{…}; 模板类BaseDecorator:*SpecificDecorred**{…};_C++_Templates - Fatal编程技术网

C++ 模板专门化继承自特定的子类 类基类{…}; 类Derived1:Base{…}; 类Derived2:Base{…}; 模板类BaseDecorator:*SpecificDecorred**{…};

C++ 模板专门化继承自特定的子类 类基类{…}; 类Derived1:Base{…}; 类Derived2:Base{…}; 模板类BaseDecorator:*SpecificDecorred**{…};,c++,templates,C++,Templates,SpecificDevered是否可以引用正在使用的特定派生类?以致 class Base { ... }; class Derived1 : Base { ... }; class Derived2 : Base { ... }; template <> class BaseDecorator<Base> : **SpecificDerived** { ... }; BaseDecorator bd-d1; 是否实例化从Derived1继承的BaseDecorat

SpecificDevered是否可以引用正在使用的特定派生类?以致

class Base { ... };
class Derived1 : Base { ... };
class Derived2 : Base { ... };

template <> class BaseDecorator<Base> : **SpecificDerived** { ... };
BaseDecorator bd-d1;
是否实例化从Derived1继承的BaseDecorator

出现这个问题是因为我需要为库类及其所有派生提供一个装饰器,但希望尽可能保持代码的干性


谢谢

如果我正确理解了您的问题,您希望BaseDecorator从特定的派生类继承

如果是这种情况,您可以这样做:

BaseDecorator<Derived1> bd-d1;

这个问题有点不清楚,但我想不出任何情况下“BaseCeracor BD1”是有效的C++语法。@ SavaVaveCK是否意味着无效的语法,因为其中一个类?还是仅仅是声明的语法?这会有什么问题?请尝试使用此名称定义存根模板,如“
template class BaseDecorator{};
”。然后继续逐字写这个声明:“<代码>基础生态器BD1;”,看看你的C++编译器喜欢这个。剧透:不太好。显然,这不是有效的C++。@ SAMVARSHIVCHIK,我假设你指连字符。至于它的其余部分,
classbase{};派生类:基{};模板类BaseDecorator{};int main(){BaseDecorator bdd1;}
编译得很好。除了BaseDecorator应该只接受基类/派生类作为模板参数之外,这几乎就是我想要的。Ie BaseDecorator不能从任意类构造,只能从基及其子类构造。@BenHogan我已经更新了我的答案。看一看。如果您喜欢,请将其标记为已接受。
#include <iostream>
#include <type_traits>

class Base {
    public:
    virtual void f1() {
        std::cout << "Base::f1" << std::endl;   
    }
};

class Derived1 : public Base {
    public:
    void f1() override {
        std::cout << "Derived1::f1" << std::endl;   
    }
};

class Derived2 : public Base {
    public:
    void f1() override {
        std::cout << "Derived2::f1" << std::endl;   
    }
};

class Derived3 {
    public:
    void f1() {
        std::cout << "Derived3::f1" << std::endl;   
    }
};

template <typename T,
          typename = typename std::enable_if<std::is_base_of<Base, T>::value>::type >
class BaseDecorator;

template <typename T>
class BaseDecorator<T>: public T {
    public:
    void f2() {
        T::f1();
    }
};

int main() {
    BaseDecorator<Derived1> bd1;
    bd1.f2();

    BaseDecorator<Derived2> bd2;
    bd2.f2();

    //BaseDecorator<Derived3> bd3; // Compilation fails !!!
    //bd3.f2(); // Compilation fails !!!

    return 0;   
}
Derived1::f1
Derived1::f2