部分专业化:使用主要模板成员 [英] Partial specialization: use the primary template members

查看:96
本文介绍了部分专业化:使用主要模板成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑

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



我想使用成员函数 f1在我的部分专用模板中的主模板的 f2()。我应该怎么办?

一个解决方案是不进行部分专门化,然后:

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

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

但是它有一个缺点,当我指示 A 的所有类别通过通用编程,我的 A 不再是类型 AA< T> ,因此我不能应用 A .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()

任何想法?

推荐答案

如何为A类创建一个定义这些方法的基类? / p>

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

这篇关于部分专业化:使用主要模板成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆