当前位置:网站首页>7.3-function-templates
7.3-function-templates
2022-07-29 05:07:00 【学习make me happy】
背景-函数重载需要写很多次
function templates
以上,编译器并不会生成机器代码。
显式实例化- 编译器会生成三个不同的函数
以上的实例化为显示实例化。
隐式实例化
#include <iostream>
#include <typeinfo>
using namespace std;
template<typename T>
T sum(T x, T y)
{
cout << "The input type is " << typeid(T).name() << endl;
return x + y;
}
int main()
{
// Implicitly instantiates product<int>(int, int)
cout << "sum = " << sum<int>(2.2f, 3.0f) << endl;
// Implicitly instantiates product<float>(float, float)
cout << "sum = " << sum(2.2f, 3.0f) << endl;
return 0;
}
输入类型为结构体-特例化
#include <iostream>
#include <typeinfo>
using namespace std;
template<typename T>
T sum(T x, T y)
{
cout << "The input type is " << typeid(T).name() << endl;
return x + y;
}
struct Point
{
int x;
int y;
};
// Specialization for Point + Point operation//这里要对比实例化,特例化不能落了<>
template<>
Point sum<Point>(Point pt1, Point pt2)
{
cout << "The input type is " << typeid(pt1).name() << endl;
Point pt;
pt.x = pt1.x + pt2.x;
pt.y = pt1.y + pt2.y;
return pt;
}
int main()
{
//Explicit instantiated functions
cout << "sum = " << sum(1, 2) << endl;
cout << "sum = " << sum(1.1, 2.2) << endl;
Point pt1 {1, 2};
Point pt2 {2, 3};
Point pt = sum(pt1, pt2);
cout << "pt = (" << pt.x << ", " << pt.y << ")" << endl;
return 0;
}
边栏推荐
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
- WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
- Scikit learn -- steps and understanding of machine learning application development
- 搭建手机APP需要用到什么服务器
- "Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
- Original code, inverse code, complement code
- How to install Office2010 installation package? How to install Office2010 installation package on computer
- Solution | get the relevant information about the current employees' highest salary in each department |
- The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
- JDBC statement + resultset introduction
猜你喜欢
随机推荐
Original code, inverse code, complement code
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
Understand activity workflow
优炫数据库启动失败,报网络错误
About the configuration and use of thymeleaf
Apache POI实现Excel导入读取数据和写入数据并导出
Ros1 dead chicken data is stored in txt and SQLite
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
TMUX essays
< El table column> place multiple pictures
About realizing page Jump of website in Servlet
Word如何查看文档修改痕迹?Word查看文档修改痕迹的方法
What servers are needed to build mobile app
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
Learn the first program of database
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
玩家访问网站自动弹窗加QQ群方法以及详细代码
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
ARFoundation从零开始3-创建ARFoundation项目
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back