IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 模板类的特例化(具体化) -> 正文阅读

[C++知识库]模板类的特例化(具体化)

模板的基础知识:

模板的实例化

????????模板并不是真正的函数或类,它仅仅是编译器用来生成函数或类的一张“图纸”。模板不会占用内存,最终生成的函数或者类才会占用内存。由模板生成函数或类的过程叫做模板的实例化,相应地,针对某个类型生成的特定版本的函数或类叫做模板的一个实例。

????????模板的实例化是按需进行的,用到哪个类型就生成针对哪个类型的函数或类,不会提前生成过多的代码。也就是说,编译器会根据传递给类型参数的实参(也可以是编译器自己推演出来的实参)来生成一个特定版本的函数或类,并且相同的类型只生成一次。实例化的过程也很简单,就是将所有的类型参数用实参代替。

模板的特例化(具体化)

????????模板是一种泛型技术,它能接受的类型是宽泛的、没有限制的,并且对这些类型使用的算法都是一样的(函数体或类体一样)。让模板能够针对某种具体的类型使用不同的算法(函数体或类体不同),这在 C++ 中是可以做到的,这种技术称为模板的显示具体化

模板的特例化(具体化)

模板的实例化

#include <iostream>
using namespace std;

template<typename T>
class Test{
public:
    Test() {
        cout << "Test(T ma)" << endl;
    }
};

int main()
{
    Test<int> t1;
    Test<char*> t2;
    Test<int(*)(int,int)> t3;

    return 0;
}

/*输出
Test(T ma)
Test(T ma)
Test(T ma)
*/

模板的完全特例化

template<typename T>
class Test{
public:
    Test() {
        cout << "Test(T ma)" << endl;
    }
};
//模板的完全特例化
template<>
class Test<char*>{
public:
    Test() {
        cout << "class Test<char*>" << endl;
    }
};


int main()
{
    Test<int> t1;
    Test<char*> t2;
    Test<int(*)(int,int)> t3;

    return 0;
}
/*
Test(T ma)
class Test<char*>  调用模板char* 的完全特例化
Test(T ma)
*/

模板的部分特例化


template<typename T>
class Test{
public:
    Test() {
        cout << "Test(T ma)" << endl;
    }
};

//模板的部分特例化
template <typename Ty>
class Test<Ty*>{
public:
    Test() {
        cout << " class Test<Ty*>" << endl;
    }
};

//模板的部分特例化
template<typename Ty, typename A1, typename A2>
class Test<Ty(*)(A1, A2)>{
public:
    Test() {
        cout << "class Test<Ty(*)(A1, A2)" << endl;
    }
};

int main()
{
    Test<int> t1;
    Test<char*> t2;
    Test<int(*)(int,int)> t3;

    return 0;
}
/*
Test(T ma)
 class Test<Ty*>
class Test<Ty(*)(A1, A2)
*/

?模板的优先级

template<typename T>
class Test{
public:
    Test() {
        cout << "Test(T ma)" << endl;
    }
};
//完全特例化
template<>
class Test<char*>{
public:
    Test() {
        cout << "class Test<char*>" << endl;
    }
};

//部分特例化
template <typename Ty>
class Test<Ty*>{
public:
    Test() {
        cout << " class Test<Ty*>" << endl;
    }
};

//部分特例化
template<typename Ty, typename A1, typename A2>
class Test<Ty(*)(A1, A2)>{
public:
    Test() {
        cout << "class Test<Ty(*)(A1, A2)" << endl;
    }
};

int main()
{
    Test<int> t1;
    Test<char*> t2;
    Test<int(*)(int,int)> t3;

    return 0;
}
/*
Test(T ma)
class Test<char*>
class Test<Ty(*)(A1, A2)
*/

从上面的结果可以看出来,模板的完全特例化? ?高于部分特例化? ? 高于模板。

????????在模板的部分特例化中,不能狭隘的认为只有模板参数列表是原来模板参数列表个数的子集,才算是部分特例化,应当把模板列表中 typename T,这个T当作一个大的类型,T <- Ty*,这样也是可以的。

function 的实现


template<typename T>
class Myfunction{};

template<typename R, typename ...A>
class Myfunction<R(A...)> {
public:
    using pfunc = R(*)(A...);
    Myfunction(pfunc func) : pfunc_(func)
    {}
    R operator()(A... arg){
        return pfunc_(arg...);
    }
private:
    pfunc pfunc_;
};

void print(string str) {
    cout << str << endl;
}

int main()
{
    Myfunction<void(string)> func1 = print;
    func1("hello world");

    return 0;
}

注意在特例化模板之前,需要有一个模板。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-29 11:55:13  更:2022-04-29 11:56:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 0:55:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码