当前位置:网站首页>模板与泛型编程之萃取-01-固定萃取技术
模板与泛型编程之萃取-01-固定萃取技术
2022-07-16 15:06:00 【发如雪-ty】
萃取这个词的英文是trait,萃取技术主要用于对模板中的各种模板参数进行管理。学习萃取技术的目的主要有以下两点。
- 了解标准库中许多萃取技术的实现方法
- 灵活运用并组合这些实现手法,写出功能更强大、更优雅和使用的代码。
本节所讲的内容是固定萃取方法,主要用于“传入一种类型,萃取出另外一种类型。”,例子如下:
template<typename T>
struct TraitsType;
template<>
struct TraitsType<char>
{
using RetType = int;
};
template<>
struct TraitsType<int>
{
using RetType = __int64;
};
template<typename T>
auto GetSum(T* begin, T* end)
{
using retType = typename TraitsType<T>::RetType;
cout << typeid(T).name() <<"==>"<< typeid(retType).name() << endl;
retType sum{
};
for (;;)
{
sum += (*begin);
if (begin == end)
{
break;
}
begin++;
}
return sum;
}
int main()
{
int a1[] = {
1,3,5,7 };
int a2[] = {
500000000,500000000,700000000 };
char a3[] = "abc";
cout << GetSum(&a1[0], &a1[3]) << endl;
cout << GetSum(&a2[0], &a2[2]) << endl;
int nsum = (int)GetSum(&a3[0],&a3[2]);
cout << nsum << endl;
system("pause");
return 0;
}
结果:
边栏推荐
猜你喜欢

844. Compare strings with backspace

CAN光端机解决泰和安TX3016C消防主机长距离联网问题

Preparing transaction:done Verifying transaction:failed RemoveError:‘requests‘ is a dependency of **

02-回顾多线程

STM32-中断优先级管理NVIC详解

【Day3】Rnn

Preparing transaction:done Verifying transaction:failed RemoveError:‘requests‘ is a dependency of **

Flask-响应

Error :Could not decode ...With “UTF-8“-encoding. Editing not possible

Huawei equipment configuration RF tuning
随机推荐
坐标转换实例讲解
Error :Could not decode ...With “UTF-8“-encoding. Editing not possible
图扑软件构建源网荷储用体系 打造循环经济2.0版本
[Luogu p6891] ビルのりけ 4 (DP) (conclusion)
844. 比较含退格的字符串
Preparing transaction:done Verifying transaction:failed RemoveError:‘requests‘ is a dependency of **
最少转机--图的广度优先遍历
JWT learning
[ CTF ] Reverse baby_re
Leetcode 1331. 数组序号转换
Use of thingjs
华为设备射频资源管理命令
CAN光端机解决泰和安TX3016C消防主机长距离联网问题
Learning path PHP -- post can't get the requested data
论文翻译解读:learning logic rules for reasoning on knowledge graphs【RNNLogic】
双动点相对运动破解
Impala advanced settings of broadcast_ BYTES_ LIMIT
How to choose clothing ERP? These questions must be asked when selecting models
[LeetCode解题报告] 423. 从英文中重建数字
每日一题·剑指Offer || 041.滑动窗口的平均值(346一样)·队列