C++模板

news/2024/5/18 23:56:02

C++模板

C++是一个面向对象编程的语言,提供了类的继承和组合机制,虽然在层次结构上很简单,但使用起来非常糟糕。C++使用关键字template,告诉编译器声明的类或者对象是一个模板。模板不是像继承和组合那样重用目标代码,而是重用源代码。容器不再包含名为 Object 的泛型基类,而是包含未指定的参数。与C语言中的宏定义有点类似,编译器可以将参数替换所需要的类型,使用模板比使用宏更加清晰和简单。

参考:Thinking in C++, Chapter 16

模板重载

模板语法:template <typename/class T> void func(T &, T &);

其中typename和class都可以表示类或者原始类型(int、double、char等),后面是函数函数返回类型void,函数名func,参数类型为T&,编译器会将T&转为所需要的类型,如int&、double&。

#include <iostream>using namespace std;/****************************/
//template overloadingtemplate <typename T>
void Swap(T &a, T &b);template <typename T>
void Swap(T *a, T *b, int);/****************************/int main()
{int a = 5, b = 7;Swap(a, b);     cout << "a:" << a << ", b:" << b <<endl;double c = 5.6, d = 7.8;Swap(c, d);cout << "c:" << c << ", d:" << d <<endl;string s("abc");string t("def");Swap(s, t);cout << s << endl;cout << t << endl;const int n = 3;int e[n] = {1,2,3};int f[n] = {4,5,6};cout << "before swap:" << endl;for(int i = 0; i < n; i++)cout << e[i] << " ";cout << endl;for(int i = 0; i < n; i++)cout << f[i] << " ";Swap(e, f, n);cout << endl;cout << "after swap:" << endl;for(int i = 0; i < n; i++)cout << e[i] << " ";cout << endl;for(int i = 0; i < n; i++)cout << f[i] << " ";return 0;
}//template overloading definition
template <class T>
void Swap(T &a, T &b){T temp;temp = a;a = b;b = temp;
}//template overloading definition
template <class T>
void Swap(T *a, T *b, int n){for(int i = 0; i < n; i++){T temp;temp = a[i];a[i] = b[i];b[i] = temp;}
}

输出如下:

显式实例化和显式具体化

显式实例化(explicit instantiation),显式具体化(explicit specialization)。

显式实例化对应的是隐式实例化,上面的列子Swap(a, b)就是利用Swap的模板生成一个int类型的实例,即隐式实例化。

显式实例化语法:template void Swap<double>(double &, double &);

显示具体化语法:

template <> void Swap(double &, double &); 或者template<> void Swap<double>(double &, double &);

并且声明了显式具体化还需要对其进行定义,有了显式具体化告诉编译器不要使用Swap的原始模板来生成一个参数类型为double型Swap实例。

C++ Primer Plus书中写到:

#include <iostream>
#include <string>using namespace std;template <typename T>
void Swap(T &a, T &b);template <typename T>
void Swap(T *a, T *b, int);template <> void Swap<int>(int &, int &);   //explicit specialization,必须定义
template void Swap<double>(double &, double &);  //explicit instantiationint main()
{double a = 3.4, b = 5.6;Swap(a, b);return 0;
}//orginal template
template <class T>
void Swap(T &a, T &b){T temp;temp = a;a = b;b = temp;
}//显示具体化 (explicit specialization)
template <> void Swap<int>(int &a, int &b){int temp;temp = a;a = b;b = temp;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/28206.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Matlab安装教程(Linux)

解压安装包 在虚拟机中,文件直接通过拖拽文件的方式将安装包拉入虚拟机时,文件通常存放在/tmp/VMwareDnD中,因此需要将存放文件位置的文件转移到/home/<用户名>/<存放目录>中 参考命令如下: mv /tmp/VMwareDnD/<文件存放目录>/* /home/<用户名>/&l…

2. 基础配置

1. 配置文件格式 1.1 配置文件自动提示功能消失解决方案 ​​ 1.2 SpringBoot配置文件加载顺序(了解) application.properties > application.yml > application.yaml 1.3 注意事项 SpringBoot核心配置文件名为application SpringBoot内置属性过多,且所有属性集中…

Qt/C++音视频开发72-倍速推流/音视频同步倍速推流/不改变帧率和采样率/低倍速和高倍速

一、前言 最近多了个新需求,需要倍速推流,推流界的扛把子obs也有倍速推流功能,最高支持到两倍速。这里所说的倍速,当然只限定在文件,只有文件才可能有倍速功能,因为也只有文件才能倍速解码播放。实时视频流是不可能倍速的,因为没有时长,有时长的才可以按照播放进度来。…

Excel求解器使用教程

添加规则求解加载项创建excel文件,点击文件点击选项选择加载项->规则求解加载项->转到选择规则求解加载项->确定求解器所在位置---数据->规划求解在excel文档中填写相关的计算公式,用来求解点击规则求解,填写对应的目标,可变单元和约束,选择求解方法来求解通过…

虚拟机创建教程

虚拟机创建 创建虚拟机的时候,选择自定义,自己来创建虚拟机在虚拟机中,选择创建16.2.X版本的虚拟机,兼容性比较好在创建虚拟机的操作系统时,选择稍后安装操作系统,实测中如果选择其他的在安装过程中会跳过系统安装的部分阶段选择对应的系统和版本选择名称和安装位置,个人…

union 和union all 使用区别

union 和union all 把 查询user表前5条数据查询user表数据从第7条数据开始,查询两条 通过union来把两个sql中的数据合并到一张表中,只查询出一条数据,会把重复的数据去掉 通过union all查询 出现出了两条数据,不会去重

TypeError: Cannot read properties of undefined (reading trim)

运行时提示:TypeError: Cannot read properties of undefined (reading trim) 问题排查: 1、确认trim()属性是否存在,这个是js 去除字符串左右空格,属性是存在的 2、确认this.form.proxy_url是否存在 3、确认确认this.form.proxy_url的值是否为undefined和null 通过排查和打…