当前位置:网站首页>函数模板学习记录
函数模板学习记录
2022-07-25 06:01:00 【熊猫小将】
概念
- 函数模板,用于适配多种参数类型,可以避免重复的代码。
1.模板函数调用方式
template <typename T >
T Add(T t1, T t2)
{
return t1 + t2;
}
template <typename T1, typename T2 >
T1 Sub(T1 t1, T2 t2)
{
return t1 + t2;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
Add(a,b); //① 调用函数模板;
Add<>(a,b); //与①效果一样,调用函数模板,此处<>无实际作用;
Add(a, a1); //此时编译器会报错,因为两个参数类型不一致;
Add<double>(a, a1); //此时<>生效,将执行的结果转为double类型
Sub(a,a1);
std::cout << Add<double>(a, a1) << std::endl;
std::cout << Add<>(a, b) << std::endl;
std::cin.get();
}
2.当普通函数和模板函数同时存在
void func()
{
std::cout << "普通函数" << std::endl;
}
template <typename T>
void func()
{
std::cout << "模板函数" << std::endl;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
func(); //调用的是普通函数,如果将上面普通函数注释掉编译器则则会报错
func<int>(); //调用的是模板函数,需要显示的传入推导参数类型,否则会报错
std::cin.get();
}
3.模板参数默认值
//默认参数类型
template <typename T1, typename T2 = int>
void funcT1(T1 a, T2 b = 123)
{
std::cout << "a:"<<a << std::endl;
std::cout << "b:" << b << std::endl;
}
template <typename T1 = int, typename T2>
void funcT2(T2 b)
{
T1 a = b;
std::cout << "b:" << b << std::endl;
std::cout << "a:" << a << std::endl;
}
template <typename T1, int T2 = 123>
void funcT3(T1 a)
{
std::cout << "a:" << a << std::endl;
std::cout << "T2:" << T2 << std::endl;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
funcT1(a,a1); //参数2默认为整型
funcT2(a1); //将浮点型强转为整型
funcT2<double>(a1); //参数1显示声明为浮点型,这样就无需类型转换了
funcT3(5);
funcT3<int,245>(5); //参数2必须为常量,在编译期间就能获取当前的值
std::cin.get();
}
4.反编译指令
即将编译生成的a文件反编译为二进制文件binary.txt
- Linux环境命令
objdump -DC a.out>binary.txt
- Win环境命令
win dumpbin /all a.obj>binary.txt
边栏推荐
- Era5 dataset description
- Programming hodgepodge (II)
- Linear algebra (3)
- Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
- Programming hodgepodge (I)
- Netease game Flink SQL platform practice
- 2020icpc Jiangxi warm up e.robot sends red packets (DFS)
- (牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
- (2022年牛客多校一)I-Chiitoitsu(期望DP)
- msys2常用配置
猜你喜欢

sqlilabs less-28~less-8a

The selection reference of Junzheng T41, T40 and T31 versions are all here

Unity Animator动画与状态机

Adaptation dynamics | in June, sequoiadb completed mutual certification with five products

Netease game Flink SQL platform practice

10、渲染基础
![(15)[驱动开发]过写拷贝](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15)[驱动开发]过写拷贝

Productivity tool in the new era -- flowus information flow comprehensive evaluation
![(15) [driver development] over written copy](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15) [driver development] over written copy

Mechanism and principle of multihead attention and masked attention
随机推荐
PostgreSQL learning 04 PG_ hint_ Plan installation and use, SQL optimization knowledge
leetcode/ 前 n 个数字二进制中 1 的个数
Leetcode/ integer division
Sword finger offer 45. arrange the array into the smallest number
Calculate BDP value and wnd value
Atof(), atoi(), atol() functions [detailed]
Leetcode 202. happy number (not happy at all)
HTB-Beep
(Niuke multi School II) j-link with arithmetic progress (least square method / three points)
y76.第四章 Prometheus大厂监控体系及实战 -- prometheus进阶(七)
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
Draw Bezier curve through screen interaction
剖析kubernetes集群内部DNS解析原理
Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
Why is it that all the games are pseudorandom and can't make true random?
Prometheus operator configures promethesrule alarm rules
R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (set the hJust parameter to show that labels of all data points are a
ROI pooling and ROI align
This is how the permission system is designed, yyds
context must be a dict rather解决