TypeList 之 Erase

本文介绍了一种使用C++模板元编程实现的基本类型列表及其操作的方法,包括获取类型列表长度、通过索引查找类型、查找类型的索引、追加类型到列表、从列表中删除指定类型以及去除重复类型等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include #include using namespace std; class NullType; namespace TL { template struct TypeList { typedef T head; typedef U tail; }; #define TYPELIST_1(T1) TypeList #define TYPELIST_2(T1,T2) TypeList #define TYPELIST_3(T1,T2,T3) TypeList #define TYPELIST_4(T1,T2,T3,T4) TypeList //----------------------------------------------------------------- //Length templatestruct Length; template<>struct Length { enum{value = 0}; }; template struct Length > { enum{value = 1 + Length::value}; }; //--------------------------------------------------------------- //利用索引查找对象 templatestruct TypeAt; template struct TypeAt,0> { typedef head Result; }; template struct TypeAt,i> { typedef typename TypeAt::Result Result; }; //--------------------------------------------------------------- //indexof templatestruct IndexOf; template struct IndexOf { enum{value = -1}; }; template struct IndexOf,T> { enum{value = 0}; }; template struct IndexOf,T> { private: enum{temp = IndexOf::value}; public: enum{value = temp == -1 ? -1 : 1 + temp}; }; //--------------------------------------------------------------- //Append templatestruct Append; template<> struct Append { typedef NullType Result; }; template struct Append { typedef TYPELIST_1(T) Result; }; template struct Append > { typedef TypeList Result; }; template struct Append,T> { typedef TypeList::Result> Result; }; //--------------------------------------------------------------- //Erase templatestruct Erase; template struct Erase { typedef NullType Result; }; template struct Erase,T> { typedef tail Result; }; template struct Erase,T> { typedef TypeList::Result> Result; }; //--------------------------------------------------------------- //NoDuplicate templatestruct NoDuplicate; template<>struct NoDuplicate { typedef NullType Result; }; template struct NoDuplicate > { private: typedef typename NoDuplicate::Result L1; typedef typename Erase::Result L2; public: typedef TypeList Result; }; } using namespace TL; int main(int argc, char *argv[]) { typedef TYPELIST_4(int,float,char,double) Test1; cout<<"Erase 前的深度为:"<::value<::Result Test2; cout<<"Erase 后的深度为:"<::value<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值