Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c++ - Partial specialization: use the primary template members

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...