当前位置:网站首页>7.3-function-templates
7.3-function-templates
2022-07-29 08:27:00 【Learn make me happy】
background - Function overloading needs to be written many times

function templates

above , The compiler does not generate machine code .
Explicitly instantiate - The compiler generates three different functions

The above instantiation is display instantiation .
Implicitly instantiate
#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;
}
The input type is structure - Specialization

#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// Here we need to compare instantiation , Special cases cannot be left behind <>
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;
}
边栏推荐
- C language macro define command exercise
- Random lottery turntable wechat applet project source code
- ML.NET相关资源整理
- DAC0832 waveform generator based on 51 single chip microcomputer
- Eggjs create application knowledge points
- Simulation of four way responder based on 51 single chip microcomputer
- MFC integration QT verification and problem handling
- Application scheme of charging pile
- 2.4G band wireless transceiver chip si24r1 summary answer
- Week 2: convolutional neural network basics
猜你喜欢

Week 1 task deep learning and pytorch Foundation

Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings

Day4: the establishment of MySQL database and its simplicity and practicality

PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled

Huawei wireless device configuration uses WDS technology to deploy WLAN services

Tensorboard use

Hal library learning notes - 8 concept of serial communication

ROS tutorial (Xavier)

Simple operation of SQL server data table

Unity多人联机框架Mirro学习记录(一)
随机推荐
110 MySQL interview questions and answers (continuously updated)
TCP - sliding window
01-01-osg GL3 environment setup
HC-SR04超声波测距模块使用方法和例程(STM32)
Clion+opencv+aruco+cmake configuration
Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
DAC0832 waveform generator based on 51 single chip microcomputer
Intelligent shelf safety monitoring system
Low cost 2.4GHz wireless transceiver chip -- ci24r1
华为无线设备配置利用WDS技术部署WLAN业务
Unity多人联机框架Mirro学习记录(一)
Segment paging and segment page combination
Detailed steps of installing MySQL 5.7 for windows
2.4G band wireless transceiver chip si24r1 summary answer
110道 MySQL面试题及答案 (持续更新)
BiSeNet v2
Collation of ml.net related resources
Proteus simulation based on 51 MCU ADC0808
Use SQL client How can the job generated by SH achieve breakpoint continuation after cancle?
搜索与回溯经典题型(八皇后)