Partial specialization: use the primary template members

让人想犯罪 __ 提交于 2019-12-06 13:44:29

问题


Consider

enum My_Enum {
    x1, x2
};

template<class T, My_Enum X>
class A {
    void f1();
    void f2();
};

template<class T>
class A<T,x1> {
    void g();
}

I want to use the member functions f1() and f2() of the primary template in my partially specialized template. What should I do ?

One solution would be not to do the partial specialization and then:

template<class T>
class AA<T> : public A<T,x1> {
    void g();
}

but it has the drawback that when I'm instatiating A<T,X>s of all sorts by generic programming, my A<T,x1> are no longer of type AA<T> and hence I cannot apply A<T,x1>.g()

Any idea ?


回答1:


How about creating a base class for class A that defines those methods?

template <class T, My_Enum X>
class A_Base {
    void f1();
    void f2();
};

template<class T, My_Enum X>
class A : public A_Base<T, X> {
};

template<class T>
class A<T,x1> : public A_Base<T, x1> {
    void g();
};



回答2:


You may create a base class:

template<class T, My_Enum X>
class BaseA {
    void f1();
    void f2();
};

template<class T, My_Enum X>
class A : BaseA<T,X> {
};

template<class T>
class A<T,x1> : BaseA<T,x1> {
   void g();
};


来源:https://stackoverflow.com/questions/18617791/partial-specialization-use-the-primary-template-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!