当前位置:网站首页>模板与泛型编程值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的任何操作都会发生在编译器。
边栏推荐
- LeetCode二叉树系列——226.翻转二叉树
- 【Pytorch】torch.argmax()用法
- Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)
- The pre-sale of the new Hyundai Paristi is open, and safety and comfort are not lost
- 我把问烂了的MySQL面试题总结了一下
- leetcode: 485. Maximum number of consecutive 1s
- 为什么要分库分表?
- I summed up the bad MySQL interview questions
- "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 paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
猜你喜欢
消息队列消息数据存储MySQL表设计
Resnet&API
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
Essential Learning for Getting Started with Unity Shader - Transparency Effect
我把问烂了的MySQL面试题总结了一下
I summed up the bad MySQL interview questions
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
The 232-layer 3D flash memory chip is here: the single-chip capacity is 2TB, and the transmission speed is increased by 50%
1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
以后面试官问你 为啥不建议使用Select *,请你大声回答他!
随机推荐
el-tooltip的使用
sentinel与nacos持久化
MySql总结
The JVM a class loader
Motion capture system for end-positioning control of flexible manipulators
leetcode:485.最大连续 1 的个数
MySQL玩到这种程度,难怪大厂抢着要!
Small test knife: Go reflection helped me convert Excel to Struct
Asynchronous processing business using CompletableFuture
公告
我把问烂了的MySQL面试题总结了一下
“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
消息队列消息数据存储MySQL表设计
Shell项目实战1.系统性能分析
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
R语言的画图代码及差异性分析[通俗易懂]
OAuth2:单点登陆客户端
MySQL【子查询】
The recently popular domestic interface artifact Apipost experience