当前位置:网站首页>模板与泛型编程值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的任何操作都会发生在编译器。
边栏推荐
- ML, DL, CV common problems sorting
- Sentinel热点参数限流
- Architecture actual combat battalion module 8 message queue table structure design
- 一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?
- A detailed guide to simulating latency with SQL/JDBC
- Five dimensions to start MySQL optimization
- [Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
- Message queue data storage MySQL table design
- leetcode:2032. 至少在两个数组中出现的值
- svn安装及使用(身体功能手册)
猜你喜欢

IDEA connects to MySQL database and uses data

I summed up the bad MySQL interview questions

C# using ComboBox control

2021 OWASP TOP 10 Vulnerability Guide

英文语法-时与态

OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry

自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统

Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")

Resnet&API

尚硅谷-JVM-内存和垃圾回收篇(P1~P203)
随机推荐
Network cable RJ45 interface pins [easy to understand]
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
微服务架构选型
Spark学习(3)-Spark环境搭建-Standalone
The pre-sale of the new Hyundai Paristi is open, and safety and comfort are not lost
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
redhat/openssl生成自签ca证书并使用
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
232层3D闪存芯片来了:单片容量2TB,传输速度提高50%
C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
A detailed guide to simulating latency with SQL/JDBC
对数字化时代的企业来说,数据治理难做,但应该去做
"Listen to me, thank you" can be said in ancient poetry?Tsinghua University has developed an artifact of "Searching Sentences According to Meaning", which can search for the famous sayings you want wi
The JVM a class loader
[Pytorch] torch.argmax() usage
Nuget package and upload tutorial
Linux bash: redis-server: command not found
[QNX Hypervisor 2.2用户手册]9.14 safety
AWS实现定时任务-Lambda+EventBridge