C++ —— 模板类具体化

C++ ------ 模板类具体化

引言

模板类具体化(特化、特例化)有两种:完全具体化部分具体化。具体化程度的类优先于具体化程度低的类,具体化的类优先于没有具体化的类。

具体化的模板类,成员函数类外实现的代码应该放在源文件中。[此文章内容了解即可]。

正常的类模板

cpp 复制代码
// 类模板
template <class T1, class T2>
class AA {
public:
    T1 m_x;
    T2 m_y;

    AA(const T1 x, const T2 y): m_x(x), m_y(y) {cout << "类模板的构造函数" << endl;}
    void show() const;
};

template <class T1, class T2>
void AA<T1, T2>::show() const {// 成员函数类外实现
    cout << "类模板:m_x = " << m_x << ", m_y = " << m_y << endl;
}

完全具体化

cpp 复制代码
// 完全具体化的意思是:为这两个通用类型参数指定具体的数据类型
template <>
class AA<int, string> {
public:
    int m_x;
    string m_y;

    AA(const int x, const string y): m_x(x), m_y(y) {cout << "完全具体化的构造函数" << endl;}
    void show() const;
};

void AA<int, string>::show() const {
    cout << "完全具体化:m_x = " << m_x << ", m_y = " << m_y << endl; 
}

部分具体化

cpp 复制代码
// 类模板部分具体化:为多个模板参数的部分参数指定具体的数据类型
// 函数模版没有部分模板具体化,类模板才有
template <class T1>
class AA<T1, string> {
    public:
    T1 m_x;
    string m_y;

    AA(const T1 x, const string y): m_x(x), m_y(y) {cout << "部分具体化的构造函数" << endl;}
    void show() const;
};

template <class T1>
void AA<T1, string>::show() const {
    cout << "部分具体化:m_x = " << m_x << ", m_y = " << m_y << endl;
}

整体参考

cpp 复制代码
#include <iostream>
using namespace std;

// 类模板
template <class T1, class T2>
class AA {
public:
    T1 m_x;
    T2 m_y;

    AA(const T1 x, const T2 y): m_x(x), m_y(y) {cout << "类模板的构造函数" << endl;}
    void show() const;
};

template <class T1, class T2>
void AA<T1, T2>::show() const {// 成员函数类外实现
    cout << "类模板:m_x = " << m_x << ", m_y = " << m_y << endl;
}

// 完全具体化的意思是:为这两个通用类型参数指定具体的数据类型
template <>
class AA<int, string> {
public:
    int m_x;
    string m_y;

    AA(const int x, const string y): m_x(x), m_y(y) {cout << "完全具体化的构造函数" << endl;}
    void show() const;
};

void AA<int, string>::show() const {
    cout << "完全具体化:m_x = " << m_x << ", m_y = " << m_y << endl; 
}

// 类模板部分具体化:为多个模板参数的部分参数指定具体的数据类型
// 函数模版没有部分模板具体化,类模板才有
template <class T1>
class AA<T1, string> {
    public:
    T1 m_x;
    string m_y;

    AA(const T1 x, const string y): m_x(x), m_y(y) {cout << "部分具体化的构造函数" << endl;}
    void show() const;
};

template <class T1>
void AA<T1, string>::show() const {
    cout << "部分具体化:m_x = " << m_x << ", m_y = " << m_y << endl;
}

int main() {
    AA<string, float> a("sfdbn", 21343.354); // 运行类模板的构造函数
    // AA<int, string> a(21343, "sfdbn"); // 运行完全具体化的构造函数
    // AA<float, string> a(21.343, "sfdbn"); // 运行部分具体化的构造函数
    
    a.show();

    return 0;
}

感谢浏览,一起学习!

相关推荐
月光在发光几秒前
22_GDB调试记录(未完成)
java·开发语言
222you2 分钟前
SpringAOP的介绍和入门
java·开发语言·spring
程序员zgh3 分钟前
代码重构 —— 读后感
运维·c语言·开发语言·c++·重构
迅为电子9 分钟前
迅为iTOP-Hi3516开发板linux驱动开发资料全面上线,构建从入门到精通的完整学习路径!
linux·驱动开发·学习
xwz小王子9 分钟前
UniBYD:超越人类示教模仿的跨实体机器人操作学习统一框架
学习·算法·机器人·跨实体
liulilittle11 分钟前
moodycamel::ConcurrentQueue 清空队列的方法论
开发语言·c++
shoubepatien12 分钟前
JAVA -- 09
java·开发语言
代码游侠16 分钟前
应用——Linux进程通信与信号处理
linux·运维·服务器·笔记·学习·信号处理
石像鬼₧魂石20 分钟前
内网渗透靶场 攻击 & 排错命令分类速查表
linux·windows·学习·ubuntu
郑州光合科技余经理26 分钟前
海外国际版同城服务系统开发:PHP技术栈
java·大数据·开发语言·前端·人工智能·架构·php