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

感谢浏览,一起学习!

相关推荐
容器( ु⁎ᴗ_ᴗ⁎)ु.。oO3 小时前
Magentic-ui 学习
学习
_李小白3 小时前
【OPENGL ES 3.0 学习笔记】延伸阅读:VAO与VBO
笔记·学习·elasticsearch
学涯乐码堂主4 小时前
GESP C++ 四级第一章:再谈函数(上)
c++·青少年编程·gesp·四级·学漄乐码青少年编程培训
微露清风4 小时前
系统性学习C++-第九讲-list类
c++·学习·list
大佬,救命!!!5 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
海边夕阳20065 小时前
【每天一个AI小知识】:什么是零样本学习?
人工智能·经验分享·学习
赵文宇(温玉)5 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
qq7422349845 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
Joker100855 小时前
仓颉自定义序列化:从原理到高性能多协议实现
开发语言
Adellle5 小时前
2.单例模式
java·开发语言·单例模式