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

感谢浏览,一起学习!

相关推荐
qq_4298796719 分钟前
省略号和可变参数模板
开发语言·c++·算法
moxiaoran575325 分钟前
uni-app学习笔记二十三--交互反馈showToast用法
笔记·学习·uni-app
优秀的颜1 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
CodeWithMe1 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi1 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
iCxhust2 小时前
Prj10--8088单板机C语言8259测试(1)
c语言·开发语言
крон5 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan6 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊6 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1186 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn