当前位置:网站首页>STL源码剖析:class template explicit specialization代码测试和理解
STL源码剖析:class template explicit specialization代码测试和理解
2022-07-30 05:50:00 【夕阳染色的坡道】
目的:对STL源码进行理解,需要通过实现来理解。这个模块,我感觉是对其中的specialization的理解,像是对模板中的类型进行实例化。比如一般的模板肯定是类型可以任意的,但是有些时候,我们需要特定的类型,模板就提供这个功能。测试代码如下:
#include<iostream>
using namespace std;
#define __STL_TEMPLATE_NULL template<>
template<class key> struct myhash //书上的代码是hash,它与我库中hash冲突
{
void operator() (){
std::cout << "hash<T>"<<std::endl;}
};
//explicit specialization
__STL_TEMPLATE_NULL struct myhash<char>
{
void operator() (){
std::cout << "hash<char>"<<std::endl;}
};
__STL_TEMPLATE_NULL struct myhash<unsigned char>
{
void operator() (){
std::cout << "hash<unsigned char>"<<std::endl;}
};
int main()
{
myhash<long> t1;
t1();
myhash<char> t2;
t2();
myhash<unsigned char> t3;
t3();
}
测试的结果如下:可以看到对于模板的调用是优先实例化的类型。
感悟:模板的功能还是非常多的,了解它对代码的阅读很有作用。
边栏推荐
猜你喜欢
多线程进阶(锁策略,自旋+CAS,Synchronized,JUC,信号量)
CTO说不建议我使用SELECT * ,这是为什么?
prometheus监控minio
The calculation proof of the intersection of the space line and the plane and its source code
空间直线到平面上的交点的计算证明及其源码
多线程进阶(CountDownLatch,死锁,线程安全集合类)
Mastering JESD204B (2) – Debugging of AD6676
Required request body is missing 问题解决
Event Delivery and Responder Chains
How to save modelsim simulation data as a file
随机推荐
Software Testing Terminology - Scenario Testing
mpich安装
Vineyard: 开源分布式内存数据管理框架
Graph Computing 101: Types, Languages, and Systems of Graph Computing
Alamofire source code analysis - POST request
图解关系数据库设计思想,这也太形象了
软件测试术语 - 场景测试
(GGG)JWT
(GGG)JWT
Ingress:从静态图分析到动态图分析
远程连接服务器的MySql
libgrape-lite on GPUs:GPU助力加速图分析任务
测试开发工程师成长日记017 - bug的生命周期
04-加壳和脱壳
大厂年薪50w+招聘具有测试平台开发能力的测试工程师
MongoDB - query
GCD timer
如何理解普吕克坐标(几何理解)
软件测试开发:发送第一封测试报告邮件
Test development engineer diary 002 - starting from 0 interface automation