当前位置:网站首页>模板与泛型编程值typelist实现
模板与泛型编程值typelist实现
2022-07-31 14:41:00 【发如雪-ty】
typelist这个概念比较系统的出现再《c++设计新思维》一书,用来操作一大堆类型的c++容器,就像c++标准库中的list容器能够为数值提供各种基本操作一样。
typelist简单来说就是一个类型容器,能够对类型提供一系列的操作。本节内容基于c++11标准实现一个typelist.
下面看看源码:
namespace TPLTS
{
template<typename...T>
class typelist
{
};
template<typename TPLT>
class front;
//写特化版本时,先书写front后面尖括号中内容,这个内容必须遵从泛化版本中template中的内容来书写,回头
//再根据尖括号中用到的内容填补template中内容
template<typename FirstItemm, typename...OtherItems>
class front<typelist<FirstItemm, OtherItems...>>
{
public:
using type = FirstItemm;
};
template<typename TPLT>
class size;
template<typename...T>
class size<typelist<T...>>
{
public:
static const inline int value = sizeof...(T);
};
template<typename TPLT>
class pop_front;
template<typename FirstItem,typename...OtherItems>
class pop_front<typelist<FirstItem, OtherItems...>>
{
public:
using type = typelist<OtherItems...>;
};
template<typename TPLT,typename NewItem>
class push_front;
template<typename...T,typename NewItem>
class push_front<typelist<T...>,NewItem>
{
public:
using type = typelist<NewItem, T...>;
};
template<typename TPLT, typename NewItem>
class push_back;
template<typename...T, typename NewItem>
class push_back<typelist<T...>, NewItem>
{
public:
using type = typelist<T..., NewItem>;
};
template<typename TPLTS,typename NewItem>
class replace_front;
template<typename FirstItem,typename...T,typename NewItem>
class replace_front<typelist<FirstItem,T...>, NewItem>
{
public:
using type = typelist<NewItem, T...>;
};
template<typename TPLTS>
class is_empty
{
public:
static const inline bool value = false;
};
template<>
class is_empty<typelist<>>
{
public:
static const inline bool value = true;
};
}
void main()
{
using tl = TPLTS::typelist<int, double, short, float>;
cout << typeid(TPLTS::front<tl>::type).name() << endl;
cout << TPLTS::size<tl>::value<< endl;
cout << typeid(TPLTS::pop_front<tl>::type).name() << endl;
cout << typeid(TPLTS::push_front<tl, long long>::type).name() << endl;
cout << typeid(TPLTS::push_back<tl, long long>::type).name() << endl;
cout << typeid(TPLTS::replace_front<tl, char>::type).name() << endl;
cout << TPLTS::is_empty<tl>::value << endl;
using t2 = TPLTS::typelist<>;
cout << TPLTS::is_empty<t2>::value << endl;
system("pause");
}
结果:
从上面的代码可以看出类模板typelist内没有任何数据,对typelist的任何操作都会发生在编译器。
边栏推荐
- element-plus虚拟表格virtual-list组件中是怎么实现清理lodash.memoize缓存的?
- 1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
- 一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?
- BigDecimal 简介,常用方法
- Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
- 海康摄像机取流RTSP地址规则说明
- ML、DL、CV常见的问题整理
- C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
- Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")
- 自适应控制——仿真实验二 用Narendra方案设计模型参考自适应系统
猜你喜欢
什么是消息队列呢?
海康摄像机取流RTSP地址规则说明
OAuth2:搭建授权服务器
Nuget打包并上传教程
MySQL【聚合函数】
Introductory UnityShader learning (2) - the rendering pipeline
Description of Hikvision camera streaming RTSP address rules
小试牛刀:Go 反射帮我把 Excel 转成 Struct
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
Why do we need to sub-library and sub-table?
随机推荐
Shell script classic case: detecting whether a batch of hosts is alive
Nuget package and upload tutorial
小试牛刀:Go 反射帮我把 Excel 转成 Struct
OAuth2:四种授权方式
Shell项目实战1.系统性能分析
Numbers that appear only once in LeetCode
Motion capture system for end-positioning control of flexible manipulators
LeetCode二叉树系列——110.平衡二叉树
How to grab configuration information for DELL SC compellent storage system
LeetCode rotate array
深入浅出边缘云 | 4. 生命周期管理
Unity Shader入门精要学习——透明效果
消息队列消息数据存储MySQL表设计
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
Sentinel流量控制
Nuget打包并上传教程
Sentinel安装与部署
IDEA连接MySQL数据库并使用数据
谷歌CTS测试(cta测试)
Tortoise speed by "template"