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

感谢浏览,一起学习!

相关推荐
inputA6 分钟前
【LwIP源码学习6】UDP部分源码分析
c语言·stm32·单片机·嵌入式硬件·网络协议·学习·udp
海尔辛6 分钟前
学习黑客5 分钟读懂Linux Permissions 101
linux·学习·安全
IsPrisoner12 分钟前
Go语言安装proto并且使用gRPC服务(2025最新WINDOWS系统)
开发语言·后端·golang
猪八戒1.012 分钟前
C++ 回调函数和Lambda表达式
c++
Python私教18 分钟前
征服Rust:从零到独立开发的实战进阶
服务器·开发语言·rust
chicpopoo23 分钟前
Python打卡DAY25
开发语言·python
源远流长jerry39 分钟前
匿名函数lambda、STL与正则表达式
c++
crazyme_61 小时前
深入掌握 Python 切片操作:解锁数据处理的高效密码
开发语言·python
真的想上岸啊1 小时前
学习51单片机01(安装开发环境)
嵌入式硬件·学习·51单片机
tan180°2 小时前
Linux进程信号处理(26)
linux·c++·vscode·后端·信号处理