Type List(C++ 模板元编程)

定义

类型列表,字面意思就是一个存储类型的列表,例如std::tuple<int, float, double, std::string>就是一个类型列表。

template<typename ...Ts> struct type_list {};

基础操作

操作约束:对于所有操作,均要求参数合法,即要求type_list中至少有一个类型,或者提供的下标不越界。

  • is_empty

  • front

  • pop_front

  • push_front

  • push_back

  • back

  • pop_back

  • reverse

  • largest(支持自定义compare)

  • merge

  • insert

is_empty

template<typename Ts>
struct is_empty_impl : std::integral_constant<bool, false> {};template<>
struct is_empty_impl<type_list<>> : std::integral_constant<bool, true> {};template<typename Ts>
constexpr static bool is_empty = is_empty_impl<Ts>::value;

Front

template<typename T> struct front_impl {};template<typename T, typename ...Ts>
struct front_impl<type_list<T, Ts...>> {using type = T;
};template<typename T> using front = front_impl<T>::type;

Pop front

template<typename T> struct pop_front_impl {};template<typename T, typename ...Ts>
struct pop_front_impl<type_list<T, Ts...>> {using type = type_list<Ts...>;
};template<typename T> using pop_front = pop_front_impl<T>::type;

Push front

template<typename Tl, typename T> struct push_front_impl {};template<typename ...Ts, typename T>
struct push_front_impl<type_list<Ts...>, T> {using type = type_list<T, Ts...>;
};template<typename Tl, typename T>
using push_front = push_front_impl<Tl, T>::type;

Push back

template<typename Tl, typename T> struct push_back_impl {};template<typename ...Ts, typename T>
struct push_back_impl<type_list<Ts...>, T> {using type = type_list<Ts..., T>;
};template<typename Tl, typename T>
using push_back = push_back_impl<Tl, T>::type;

Back

template<typename Tl> struct back_impl {};template<typename T, typename ...Ts>
struct back_impl<type_list<T, Ts...>> : back_impl<type_list<Ts...>> {};template<typename T>
struct back_impl<type_list<T>> { using type = T; };template<typename Tl> using back = back_impl<Tl>::type;

Pop back

template<typename Tl> struct pop_back_impl {};template<typename T, typename ...Ts>
struct pop_back_impl<type_list<T, Ts...>>: push_front_impl<typename pop_back_impl<type_list<Ts...>>::type, T> {};template<typename T>
struct pop_back_impl<type_list<T>> { using type = type_list<>; };template<typename Tl> using pop_back = pop_back_impl<Tl>::type;

Reverse

template<typename Tl, bool empty = is_empty<Tl>> struct reverse_impl {};template<typename Tl>
struct reverse_impl<Tl, false>: push_back_impl<typename reverse_impl<pop_front<Tl>>::type, front<Tl>> {};template<typename Tl>
struct reverse_impl<Tl, true> { using type = type_list<>; };template<typename Tl>
using reverse = reverse_impl<Tl>::type;

Largest (可接收自定义类型比较函数:参考compare实现)

template<bool judge, typename T1, typename T2>
struct type_choose {using type = T2;
};template<typename T1, typename T2>
struct type_choose<true, T1, T2> {using type = T1;
};template<typename T1, typename T2>
struct compare : std::integral_constant<bool, false> {};template<typename T1, typename T2>
requires (sizeof(T1) > sizeof(T2))
struct compare<T1, T2> : std::integral_constant<bool, true> {};template<typename Tl, template<typename ...> typename C> struct largest_impl {};template<typename T, template<typename ...> typename C>
struct largest_impl<type_list<T>, C> {using type = T;
};template<typename T, typename ...Ts, template<typename ...> typename C>
struct largest_impl<type_list<T, Ts...>, C>: type_choose<C<T, typename largest_impl<type_list<Ts...>, C>::type>::value,T, typename largest_impl<type_list<Ts...>, C>::type> {};template<typename Tl, template<typename ...> typename C>
using largest = largest_impl<Tl, C>::type;

merge

template<typename Tl1, typename Tl2> struct merge_impl {};template<typename ...Ts1, typename ...Ts2>
struct merge_impl<type_list<Ts1...>, type_list<Ts2...>> {using type = type_list<Ts1..., Ts2...>;
};template<typename Tl1, typename Tl2>
using merge = merge_impl<Tl1, Tl2>::type;

insert

两个子模板会在insert_impl<type_list<Ts...>, 0, T> sizeof...(Ts) > 0的时候冲突,所以加上一个requires约束一下。

template<typename Tl, int index, typename T> struct insert_impl {};template<typename Tf, typename ...Ts, int index, typename T>
requires (index > 0)
struct insert_impl<type_list<Tf, Ts...>, index, T>: push_front_impl<typename insert_impl<type_list<Ts...>, index - 1, T>::type, Tf> {};template<typename ...Ts, typename T>
struct insert_impl<type_list<Ts...>, 0, T>: push_front_impl<type_list<Ts...>, T> {};template<typename Tl, int index, typename T>
using insert = insert_impl<Tl, index, T>::type;

TEST

符合TDD,简单测试一下:

int main() {// is empty: 1 0 0std::cout << "is empty: ";std::cout << is_empty<type_list<>> << " "<< is_empty<type_list<int>> << " "<< is_empty<type_list<int, float, double>> << "\n\n";// front: 1 0std::cout << "front: ";std::cout << std::is_same_v<int, front<type_list<int, float>>> << " "<< std::is_same_v<float, front<type_list<int, float>>> << "\n\n";// pop front: 1 0 1std::cout << "pop front: ";std::cout << std::is_same_v<type_list<>, pop_front<type_list<int>>> << " "<< std::is_same_v<type_list<int>, pop_front<type_list<int>>> << " "<< std::is_same_v<type_list<int>, pop_front<type_list<float, int>>> << "\n\n";// push front: 1 0 1std::cout << "push front: ";std::cout << std::is_same_v<type_list<int>, push_front<type_list<>, int>> << " "<< std::is_same_v<type_list<int, float>, push_front<type_list<int>, float>> << " "<< std::is_same_v<type_list<float, int>, push_front<type_list<int>, float>> << "\n\n";// push back: 1 1 0std::cout << "push back: ";std::cout << std::is_same_v<type_list<int>, push_back<type_list<>, int>> << " "<< std::is_same_v<type_list<int, float>, push_back<type_list<int>, float>> << " "<< std::is_same_v<type_list<float, int>, push_back<type_list<int>, float>> << "\n\n";// back: 1 0 1std::cout << "back: ";std::cout << std::is_same_v<int, back<type_list<int>>> << " "<< std::is_same_v<int, back<type_list<int, float>>> << " "<< std::is_same_v<float, back<type_list<int, float>>> << "\n\n";// pop back: 1 1 0std::cout << "pop back: ";std::cout << std::is_same_v<type_list<>, pop_back<type_list<int>>> << " "<< std::is_same_v<type_list<float>, pop_back<type_list<float, int>>> << " "<< std::is_same_v<type_list<int>, pop_back<type_list<float, int>>> << "\n\n";// reverse: 1 0 1 1std::cout << "reverse: ";std::cout << std::is_same_v<type_list<>, reverse<type_list<>>> << " "<< std::is_same_v<type_list<int, float>, reverse<type_list<int, float>>> << " "<< std::is_same_v<type_list<float, int>, reverse<type_list<int, float>>> << " "<< std::is_same_v<type_list<int, float, double>, reverse<type_list<double, float, int>>> << "\n\n";// largest: 1, 0, 1// char, short, int32_t, int64_t, doublestd::cout << sizeof(char) << " " << sizeof(short) << " " << sizeof(int32_t)<< " " << sizeof(int64_t) << " " << sizeof(double) << "\n";using type1 = type_list<char, short, int32_t, int64_t, double>;using type2 = type_list<char, short, int32_t, double, int64_t>;std::cout << "largest: ";std::cout << std::is_same_v<double, largest<type1, compare>> << " "<< std::is_same_v<double, largest<type2, compare>> << " "<< std::is_same_v<int64_t, largest<type2, compare>> << "\n\n";// merge: 1 1 1 0std::cout << "merge: ";std::cout << std::is_same_v<type_list<int>, merge<type_list<>, type_list<int>>> << " "<< std::is_same_v<type_list<int>, merge<type_list<int>, type_list<>>> << " "<< std::is_same_v<type_list<int, float>, merge<type_list<int>, type_list<float>>> << " "<< std::is_same_v<type_list<int, float>, merge<type_list<float>, type_list<int>>> << "\n\n";// insert: 1 1 1std::cout << "insert: ";std::cout << std::is_same_v<type_list<int>, insert<type_list<>, 0, int>> << " "<< std::is_same_v<type_list<int, float, double>, insert<type_list<int, double>, 1, float>> << " "<< std::is_same_v<type_list<int, float, double>, insert<type_list<int, float>, 2, double>> << "\n";return 0;
}

Algorithm

Insert sort

维护尾部的有序性,每次加入一个新值,同时保证尾部的有效性,那么先实现一个insert_in_sorted<type_list<Ts...>, T>

  • 找到第一个满足C的位置,并且在这个值前面插入
template<typename Tl, typename T, template<typename ...> typename C,bool empty = is_empty<Tl>>
struct insert_in_sorted_impl {};template<typename ...Ts, typename T, template<typename ...> typename C>
struct insert_in_sorted_impl<type_list<Ts...>, T, C, false>: type_choose<(C<front<type_list<Ts...>>, T>::value),push_front<type_list<Ts...>, T>,push_front<typename insert_in_sorted_impl<pop_front<type_list<Ts...>>, T, C>::type,front<type_list<Ts...>>>> {};template<typename ...Ts, typename T, template<typename ...> typename C>
struct insert_in_sorted_impl<type_list<Ts...>, T, C, true> {using type = type_list<T>;
};template<typename Tl, typename T, template<typename ...> typename C>
using insert_in_sorted = insert_in_sorted_impl<Tl, T, C>::type;
  • 排序实现
template<typename Tl, template<typename ...> typename C,bool empty = is_empty<Tl>> struct insert_sort_impl {};template<typename ...Ts, template<typename ...> typename C>
struct insert_sort_impl<type_list<Ts...>, C, true> {using type = type_list<>;
};template<typename ...Ts, template<typename ...> typename C>
struct insert_sort_impl<type_list<Ts...>, C, false>: insert_in_sorted_impl<typename insert_sort_impl<pop_front<type_list<Ts...>>, C>::type,front<type_list<Ts...>>, C> {};template<typename Tl, template<typename ...> typename C>
using insert_sort = insert_sort_impl<Tl, C>::type;
  • 测试一下结果对不对
int main() {// insert in sorted: 1 1 1 1 1std::cout << "insert in sorted: ";std::cout << std::is_same_v<type_list<int32_t>, insert_in_sorted<type_list<>, int32_t , compare>> << " "<< std::is_same_v<type_list<char, short, int32_t, int64_t>, insert_in_sorted<type_list<char, short, int32_t>, int64_t, compare>> << " "<< std::is_same_v<type_list<char, short, int32_t, int64_t>, insert_in_sorted<type_list<char, short, int64_t>, int32_t, compare>> << " "<< std::is_same_v<type_list<char, short, int32_t, float, int64_t>, insert_in_sorted<type_list<char, short, int32_t, int64_t>, float, compare>> << " "<< std::is_same_v<type_list<char, int32_t, long long, float, int64_t>, insert_in_sorted<type_list<char, long long, float, int64_t>, int32_t, compare>> << "\n";// insert sort: 1 1 1 1std::cout << "insert sort: ";std::cout << std::is_same_v<type_list<>, insert_sort<type_list<>, compare>> << " "<< std::is_same_v<type_list<char>, insert_sort<type_list<char>, compare>> << " "<< std::is_same_v<type_list<float, int>, insert_sort<type_list<int, float>, compare>> << " ";// 非稳定排序,相同大小的类型,初始在前面的会跑到后面去using type_list_sort = type_list<short int, char, short, int, long long, unsigned int, unsigned, unsigned long long, unsigned char>;using type_list_sort_ans = type_list<unsigned char, char, short, short int, unsigned, unsigned int, int, unsigned long long, long long>;std::cout << std::is_same_v<type_list_sort_ans, insert_sort<type_list_sort, compare>> << "\n";return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/129780.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pix2tex - LaTeX OCR 安装使用记录

系列文章目录 文章目录 系列文章目录前言一、安装二、使用三、如果觉得内容不错&#xff0c;请点赞、收藏、关注 前言 项目地址&#xff1a;这儿 一、安装 版本要求 Python: 3.7 PyTorch: >1.7.1 安装&#xff1a;pip install "pix2tex[gui]" 注意&#xff1a…

会声会影2024对比2023变化以及功能对比

全新会声会影2024版本现已登场&#xff0c;小伙伴们相信已经急不可待地想知道2024版到底有哪些新功能。对比2023版本&#xff0c;会声会影2024版本有没有功能的增强&#xff1f;事不宜迟&#xff0c;现在就让我们一起来看看会声会影2024对比2023的变化&#xff0c;包括功能对比…

华为是怎么做全面预算管理的?

大家好&#xff0c;我是老原。 在之前的文章分享中&#xff0c;都穿插着一个很重要但是不被你们重视的的部分&#xff0c;就是预算管理这块。 有很多新手项目经理在做计划的时候&#xff0c;发现自己设备和步骤都不熟悉&#xff0c;根本无从下手&#xff0c;不知道怎么做。 …

XXL-JOB默认accessToken身份绕过RCE漏洞复现 [附POC]

文章目录 XXL-JOB默认accessToken身份绕过RCE漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 XXL-JOB默认accessToken身份绕过RCE漏洞复现 [附POC] 0x01 前言 免责声明&#xff1a;请勿利用文章内的相关技术从…

Java--网络通信

1.端口Port&#xff1a; 通常计算机上提供了HTTP,FTP等多种服务&#xff0c;客户机通过不同的端口来确定连接到服务器的哪项服务上。 2.套接字Socket&#xff1a; 套接字Socket用于将应用程序与端口连接起来。套接字是一个假想的链接装置。 3.InetAddress类 java.net包中的Ine…

学习NFS

文章目录 一、NFS介绍二、配置文件三、例子 一、NFS介绍 NFS&#xff08;Network File System&#xff09;网络文件系统&#xff0c;主要用于Linux系统上实现文件共享的一种协议&#xff0c;其客户端主要是Linux。没有用户认证机制&#xff0c;且数据在网络上传送的时候是明文…

制作一个ros2机器人需要学习的课本(还不全面)

1《C语言》---这个是基础200页左右 2《C》-----500-600页 3《高等数学》-----没有这个无法计算动态电路 4《电路分析》-----没有这个没法设计硬件电路 5《英语5000词汇》最少也得达到美国小学生毕业时候的词汇水平5000词汇量 6《ros1》因为ros2没有一本中文课本---有那么一…

【C#】获得所有可见窗口信息

【背景】 由于自己的瘦客户端上的Windows自带截图软件功能被阉割&#xff0c;所以自己写了一个&#xff0c;其中有窗口截图功能&#xff0c;涉及到获得所有可见窗口的信息。 【代码】 public WindowInfo[] GetAllDesktopWindows(){//用来保存窗口对象 列表List<WindowInf…

VSCode 如何设置背景图片

VSCode 设置背景图片 1.打开应用商店&#xff0c;搜索 background &#xff0c;选择第一个&#xff0c;点击安装。 2. 安装完成后点击设置&#xff0c;点击扩展设置。 3.点击在 settings.json 中编辑。 4.将原代码注释后&#xff0c;加入以下代码。 // { // "workben…

浅谈PHP框架中类成员方法的类类型形参是怎么利用ReflectionClass反射类自动实例化的(应该是全网首发)

说明 1. 或许是全网首发&#xff0c;我翻过很多文章&#xff0c;从未有一个博主讲过这个东西&#xff0c;很多博主只讲了IOC、DI和反射机制的常见用法&#xff0c;因类类型形参反射的巧妙用法有相当高的难度和学习盲区&#xff0c;所以从未有人讲过类类型的形参它怎么就被自动…

SpringCloudTencent(上)

SpringCloudTencent 1.PolarisMesh介绍2.北极星具备的功能3.北极星包含的组件4.功能特性1.服务管理1.服务注册2.服务发现3.健康检查 2.配置管理 5.代码实战1.环境准备2.服务注册与发现3.远程调用 1.PolarisMesh介绍 1.北极星是腾讯开源的服务治理平台&#xff0c;致力于解决分…

RESTful接口实现与测试

目录标题 是什么&#xff1f;设计风格HTTP协议四种传参方式常用注解RequestBody与ResponseBodyRequestMapping注解RestController与ControllerPathVariable 与RequestParam 接受复杂嵌套对象参数Http数据转换的原理自定义HttpMessageConverter统一规划接口响应的数据格式实战&a…

【使用Python编写游戏辅助工具】第一篇:概述

引言 欢迎阅读本系列文章&#xff0c;本系列将带领读者朋友们使用Python来实现一个简单而有趣的游戏辅助工具。 写这个系列的缘由源自笔者玩了一款游戏。正巧&#xff0c;笔者对Python编程算是有一定的熟悉&#xff0c;且Python语言具备实现各种有趣功能的能力&#xff0c;因…

MySQL复习总结(一):基础篇

文章目录 一、MySQL概述二、SQL语句2.1 SQL分类2.2 DDL语言2.2.1 数据库操作2.2.2 表操作:通用2.2.3 表操作:修改2.2.4 表操作:删除 2.3 DML语言2.3.1 添加数据2.3.2 修改数据2.3.3 删除数据 2.4 DQL语言2.5 DCL语言 三、函数四、约束五、多表查询5.1 多表关系 六、事务6.1 事务…

Compose-Multiplatform在Android和iOS上的实践

本文字数&#xff1a;4680字 预计阅读时间&#xff1a;30分钟 01 简介 之前我们探讨过KMM&#xff0c;即Kotlin Multiplatform Mobile&#xff0c;是Kotlin发布的移动端跨平台框架。当时的结论是KMM提倡将共有的逻辑部分抽出&#xff0c;由KMM封装成Android(Kotlin/JVM)的aar和…

【LeetCode:80. 删除有序数组中的重复项 II | 双指针】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

DbUtils + Druid 实现 JDBC 操作 --- 附BaseDao

文章目录 Apache-DBUtils实现CRUD操作1 Apache-DBUtils简介2 主要API的使用2.1 DbUtils2.2 QueryRunner类2.3 ResultSetHandler接口及实现类 3 JDBCUtil 工具类编写3.1 导包3.2 编写配置文件3.3 编写代码 4 BaseDao 编写 Apache-DBUtils实现CRUD操作 1 Apache-DBUtils简介 com…

pytorch+LSTM实现使用单参数预测,以及多参数预测(代码注释版)

开发前准备&#xff1a; 环境管理&#xff1a;Anaconda python: 3.8 显卡&#xff1a;NVIDIA3060 pytorch: 到官网选择conda版本&#xff0c;使用的是CUDA11.8 编译器&#xff1a; PyCharm 简述&#xff1a; 本次使用seaborn库中的flights数据集来做试验&#xff0c;我们通过…

代码训练营第59天:动态规划part17|leetcode647回文子串|leetcode516最长回文子序列

leetcode647&#xff1a;回文子串 文章讲解&#xff1a;leetcode647 leetcode516&#xff1a;最长回文子序列 文章讲解&#xff1a;leetcode516 DP总结&#xff1a;动态规划总结 目录 1&#xff0c;leeetcode647 回文子串。 2&#xff0c;leetcode516 最长回文子串&#xff1…

多模态论文阅读之BLIP

BLIP泛读 TitleMotivationContributionModel Title BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation Motivation 模型角度&#xff1a;clip albef等要么采用encoder-base model 要么采用encoder-decoder model.…