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

感谢浏览,一起学习!

相关推荐
电商API&Tina12 分钟前
Python请求淘宝商品评论API接口全指南||taobao评论API
java·开发语言·数据库·python·json·php
学嵌入式的小杨同学15 分钟前
【嵌入式 C 语言实战】交互式栈管理系统:从功能实现到用户交互全解析
c语言·开发语言·arm开发·数据结构·c++·算法·链表
txinyu的博客16 分钟前
static_cast、const_cast、dynamic_cast、reinterpret_cast
linux·c++
HyperAI超神经21 分钟前
具身智能资源汇总:机器人学习数据集,在线体验世界建模模型,英伟达/字节/小米等最新研究论文
人工智能·深度学习·学习·机器学习·机器人·ai编程·图形生成
“αβ”30 分钟前
TCP相关实验
运维·服务器·网络·c++·网络协议·tcp/ip·udp
小杍随笔32 分钟前
【Rust Cargo 目录迁移到 D 盘:不改变安装路径和环境变量的终极方案】
开发语言·后端·rust
Henry Zhu1231 小时前
Qt Model/View架构详解(五):综合实战项目
开发语言·qt·架构
孞㐑¥1 小时前
算法—滑动窗口
开发语言·c++·经验分享·笔记·算法
AI-小柒1 小时前
从零入门大语言模型(LLM):系统学习路线与实践指南
大数据·开发语言·人工智能·学习·信息可视化·语言模型·自然语言处理
hhy_smile1 小时前
Python environment and installation
开发语言·python