当前位置:网站首页>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;
}
边栏推荐
- 输入的查询SQL语句,是如何执行的?
- JS (in ES6) sync & await understanding
- What servers are needed to build mobile app
- Five correlation analysis, one of the most important skills of data analysts
- AUTOSAR from introduction to proficiency 100 lectures (78) -autosar-dem module
- The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
- The difference between the two ways of thread implementation - simple summary
- 带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
- Glory 2023 push, push code ambubk
- Unity metaverse (III), protobuf & socket realize multi person online
猜你喜欢

Use annotation test in idea

Mysql的自连接和联合查询

The method and detailed code of automatically pop-up and QQ group when players visit the website

Learn the first program of database

传奇服务端如何添加地图

【文件下载】Easyexcel快速上手

2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结

ARFoundation从零开始3-创建ARFoundation项目

输入的查询SQL语句,是如何执行的?

How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
随机推荐
tmux随笔
Force deduction ----- sort odd and even subscripts respectively
【文件下载】Easyexcel快速上手
7.2-function-overloading
Deadlock to be resolved
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
如何安装office2010安装包?office2010安装包安装到电脑上的方法
[sudden] solve remote: support for password authentication was removed on August 13, 2021. please use a perso
Getting started with solidity
Mysql的自连接和联合查询
JS daily question (10)
Numpy Foundation
Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
优炫数据库启动失败,报网络错误
时间序列分析的表示学习时代来了?
玩家访问网站自动弹窗加QQ群方法以及详细代码
Wechat picture identification
Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法