范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文

C模板编程自定义创建元组(tuple)

  一、各个模块
  1、将数组转换为指针// decay_array to pointer template auto decay_array(T(&array)[N]) { 	return array; }
  2、定义初始模板类型
  1)元素类型为T
  2)容器类型为T
  3)是否为字符数组类型:否
  4)数组大小:0// primary class template template struct container_element_st { 	using type = T; 	using element_type = T; 	static constexpr bool is_character_array = false; 	static constexpr size_t array_count = 0; };
  3、定义字符数组类型
  1)元素类型为char
  2)容器类型为std::string
  3)是否为字符数组类型:是
  4)数组大小:Ntemplate struct container_element_st	 // non reference { 	using type = char; 	using element_type = std::string; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = N; };
  4、定义字符指针类型
  1)元素类型为char
  2)容器类型为std::string
  3)是否为字符数组类型:是
  4)数组大小:0template<> struct container_element_st	 // non reference { 	using type = char; 	using element_type = std::string; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = 0; };
  5、定义非字符类型数组
  1)元素类型为T
  2)容器类型为std::vector
  3)是否为字符数组类型:否// array but not character array template struct container_element_st { 	using type = T; 	using element_type = std::vector; 	static constexpr bool is_character_array = false; };
  6、定义初始化列表类型
  1)元素类型为T
  2)容器类型为std::vector
  3)是否为字符数组类型:否// from initializer_list to vector conversion template struct container_element_st> { 	using type = T; 	using element_type = std::vector; 	static constexpr bool is_character_array = false; };
  7、定义各种类型
  1)是否为字符数组类型
  2)获取字符数组的大小
  3)获取字符数组的类型,char or wchar_t
  4)获取容器类型// get whethere character array template constexpr auto is_character_array_v = container_element_st>::is_character_array;  // get array size template constexpr auto array_count_v = container_element_st< tft::remove_const_reference_t>::array_count;  // define element type template using element_flat_t = typename container_element_st< tft::remove_const_reference_t>::type;  // define container element type template using container_element_t = typename container_element_st< tft::remove_const_reference_t>::element_type;
  8、创建自定义元组
  创建元组的时候,元组的元素可以是元组,可以使vector,也可以是各种基础类型元素。template auto make_tuple(Type&& first, Types&&... args) { 	using container_t = std::tuple, container_element_t...>; 	 	return container_t{ element_to_container(std::forward(first)), 		element_to_container(std::forward(args))... }; }
  9、获取自定义元组中的元素
  分别可以获取一层元组的元素、两层元组的元素、三层元组的元素。template decltype(auto) get(CntrType& cntr) { 	return std::get(cntr); }  template decltype(auto) get(CntrType& cntr) { 	if constexpr (sizeof...(Indices) > 0) 		return get(get(cntr)); 	else 		return get(cntr); }  template decltype(auto) get(CntrType& cntr, IndexType index) { 	if constexpr (sizeof...(Indices) > 0) 		return get(get(cntr))[index]; 	else 		return get(cntr)[index]; }
  二、整体代码#include "tft_type_lib.h" #include "tpf_type_util.h"  auto stream = tpf::stream();			// stream auto nl = stream.get_console_out();	  // new line  // decay_array to pointer template auto decay_array(T(&array)[N]) { 	return array; }  // primary class template template struct container_element_st { 	using type = T; 	using element_type = T; 	static constexpr bool is_character_array = false; 	static constexpr size_t array_count = 0; };  template struct container_element_st	 // non reference { 	using type = char; 	using element_type = std::string; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = N; };  template<> struct container_element_st	 // non reference { 	using type = char; 	using element_type = std::string; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = 0; };  template struct container_element_st	 // non reference { 	using type = wchar_t; 	using element_type = std::wstring; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = N; };  template<> struct container_element_st	 // non reference { 	using type = wchar_t; 	using element_type = std::wstring; 	static constexpr bool is_character_array = true; 	static constexpr size_t array_count = 0; };  // array but not character array template struct container_element_st { 	using type = T; 	using element_type = std::vector; 	static constexpr bool is_character_array = false; };  template struct container_element_st { 	using type = T; 	using element_type = std::vector; 	static constexpr bool is_character_array = false; };  // from initializer_list to vector conversion template struct container_element_st> { 	using type = T; 	using element_type = std::vector; 	static constexpr bool is_character_array = false; };  // get whethere character array template constexpr auto is_character_array_v = container_element_st>::is_character_array;  // get array size template constexpr auto array_count_v = container_element_st< tft::remove_const_reference_t>::array_count;  // define element type template using element_flat_t = typename container_element_st< tft::remove_const_reference_t>::type;  // define container element type template using container_element_t = typename container_element_st< tft::remove_const_reference_t>::element_type;  // create a vector template auto make_vector(Type&& first, Types&&... args) { 	// determine whether type argument is an array 	if constexpr (std::is_array_v>) 	{ 		// this is to allow only character array 		static_assert(is_character_array_v, "should be character array");  		using element_t = container_element_t; 		using container_t = std::vector;  		size_t size = array_count_v; 		const auto& last_character = first[size - 1];  		// last character is NULL character 		if (element_flat_t{} == last_character) 			return container_t{ decay_array(first),decay_array(args)... }; 		else 		{ 			return	container_t{ element_t{std::cbegin(first),std::cend(first)}, 					element_t{std::cbegin(args),std::cend(args)}... }; 		} 	} 	else 	{ 		using element_t = tft::remove_const_reference_t; 		return std::vector{ std::forward(first), std::forward(args)... }; 	} }  // 创建一个容器 template< template class CntrType, typename Type, typename... Types> auto make_container(Type&& first, Types&&... args) { 	if constexpr (std::is_array_v>) 	{ 		// this is to allow only character only 		static_assert(is_character_array_v, "should be character array");  		using element_t = container_element_t; 		using container_t = CntrType;  		size_t size = array_count_v; 		const auto& last_character = first[size - 1];  		// last character is NULL character 		if (element_flat_t{} == last_character) 			return container_t{ decay_array(first),decay_array(args)... }; 		else 		{ 			return	container_t{ element_t{std::cbegin(first),std::cend(first)}, 					element_t{std::cbegin(args),std::cend(args)}... }; 		} 	} 	else 	{ 		using element_t = tft::remove_const_reference_t; 		return CntrType{ std::forward(first), std::forward(args)... }; 	} }  template void print_type(T&& arg) { 	stream << "Type of T:" << Tpf_GetTypeName(T) 		<< ", Type of arg: " << Tpf_GetTypeCategory(decay_array(arg)) << nl; }  void simple_tip() { 	int a = 1; 	std::vector v1{ 1,2,3,4,5 }; 	auto v2 = make_vector(a, 2, 3, 4, 5, 6, 7, 8, 9, 10); 	stream << "v1 = " << v1 << nl; 	stream << "v2 = " << v2 << nl;  	auto v3 = make_vector("123", "tomas kim", "han", "saimei", "shuang"); 	stream << "v3 = " << v3 << nl; }  void simple_tip_advance() { 	int a = 1; 	std::vector v1{ 1,2,3,4,5 }; 	auto v2 = make_container(a, 2, 3, 4, 5, 6, 7, 8, 9, 10); 	stream << "v1 = " << v1 << nl; 	stream << "v2 = " << v2 << nl;  	auto v3 = make_container("123", "tomas kim", "han", "saimei", "shuang"); 	stream << "v3 = " << v3 << nl; }  /* 	we have to convert const char* to  std::string 	const wchar_t* to std::wstring 	convert array,int[] to std::vector 	std::initializer_list<> to std::vector<> */  template auto element_to_container(Type&& arg) { 	// array  such as char[N],int[N] ect. 	if constexpr (std::is_array_v>) 	{ 		if constexpr (is_character_array_v) 		{ 			// character array ,we convert  			// const char* to std::string 			// const wchar_t* to std::wstring 			// char[N] to std::string 			// wchar_t[N] to std::wstring 			using element_t = container_element_t; 			size_t size = array_count_v; 			const auto& last_character = arg[size - 1]; 			 			// element_flat_t returns either char or wchar_t 			// testing if zero terminated character 			if (element_flat_t{} == last_character) 				return element_t{ decay_array(arg) };// decay_array() convert char[] to char* 			else // not zero terminating character array 				return element_t{ std::cbegin(arg),std::cend(arg) }; 		} 		else 		{ 			// non character array 			using element_t = container_element_t; 			return element_t{ std::cbegin(arg),std::cend(arg) }; 		} 	} 	else 	{ 		// non array case 		using element_t = container_element_t; 		return element_t{ std::forward(arg) }; 	} }  // this belongs to non-deduced context template auto element_to_container(std::initializer_list& lst) { 	// decltype(lst),decltype(),get variable type 	// using element_t = container_element_t>; 	using element_t = container_element_t;  	// conversion from std::initializer_list to std::vector 	return element_t{ lst }; }  // this belongs to non-deduced context template auto element_to_container(std::initializer_list&& lst) { 	// decltype(lst),decltype(),get variable type 	// using element_t = container_element_t>; 	using element_t = container_element_t;  	// conversion from std::initializer_list to std::vector 	return element_t{ std::forward>(lst) }; }  template auto make_tuple(Type&& first, Types&&... args) { 	using container_t = std::tuple, container_element_t...>; 	 	return container_t{ element_to_container(std::forward(first)), 		element_to_container(std::forward(args))... }; }  template decltype(auto) get(CntrType& cntr) { 	return std::get(cntr); }  template decltype(auto) get(CntrType& cntr) { 	if constexpr (sizeof...(Indices) > 0) 		return get(get(cntr)); 	else 		return get(cntr); }  template decltype(auto) get(CntrType& cntr, IndexType index) { 	if constexpr (sizeof...(Indices) > 0) 		return get(get(cntr))[index]; 	else 		return get(cntr)[index]; }  void test_element_to_container() { 	using literal_string_t = const char*;  	auto str1 = element_to_container("This is literal string"); 	stream << "str1 = " << str1 << " Type: " 		<< Tpf_GetTypeCategory(str1) << nl;  	int array[]{ 1,2,3,4,5 }; 	auto vtr1 = element_to_container(array); 	stream << "vtr1 = " << vtr1 << " Type: " 		<< Tpf_GetTypeCategory(vtr1) << nl;  	auto lst = element_to_container({ 3.1,5.0,6.0 }); 	stream << "lst = " << lst << " Type: " 		<< Tpf_GetTypeCategory(lst) << nl;  }  void test_make_tuple() { 	const char* msg1 = "Literal string"; 	const wchar_t* msg2 = L"wide character string"; 	auto t1 = make_tuple(msg1,"han", "shuang", "shuang han", 20, "sia mie",msg2); 	stream << "t1 = " << t1 << /*" Type: " 		<< Tpf_GetTypeCategory(t1) <<*/ nl;  	using t1_0_th_t = std::tuple_element_t<0, decltype(t1)>; 	stream << "t1-0-th element: " << std::get<0>(t1) << "  Type: " 		<< Tpf_GetTypeName(t1_0_th_t) << nl;  	using t1_4_th_t = std::tuple_element_t<4, decltype(t1)>; 	stream << "t1-4-th element: " << std::get<4>(t1) << "  Type: " 		<< Tpf_GetTypeName(t1_4_th_t) << nl;  	using t1_6_th_t = std::tuple_element_t<6, decltype(t1)>; 	stream << "t1-6-th element: " << std::get<6>(t1) << "  Type: " 		<< Tpf_GetTypeName(t1_6_th_t) << nl; }  void test_advanced_make_tuple() { 	auto t1 = make_tuple( 		make_tuple(1, 3.14, "han"), 		make_tuple(make_vector(1, 2, 3, 4, 5), make_vector(3.0, 5.0, 6.1), 			make_tuple("han", "s shuang", 40.2f)), 		make_vector("james", "seveten"));  	stream << t1 << nl; 	stream << get<0>(t1) << nl; 	stream << get<1>(t1) << nl;  	stream << get<1, 0>(t1) << nl; 	stream << get<1, 1>(t1) << nl; 	stream << get<1, 2>(t1) << nl;  	stream << nl;  	//get<1, 0>(t1) is a vector 	for (const auto& e : get<1, 0>(t1)) 		stream << e << " "; 	stream << nl; }  int main() { 	//test_element_to_container(); 	//test_make_tuple(); 	test_advanced_make_tuple(); 	return 0; }

独显芯片显神威双芯实力派通吃各大手游iQOONeo6手机评测随着手机市场的不断更新换代,越来越多的手机开始走入消费者的视野。然而要说一款手机中最重要的部分,那可能是非芯片莫属了。纵观今年的手机市场,骁龙芯片可以说是安卓阵营里的独一家,拥有着中国机床产业告别卡脖子,5年造全球首台智能化数控机床,有实力图为智能机床机床被称为工业之母,对制造业起着至关重要的作用,中国作为世界工厂,自然也需要掌握自己的工业机床技术,如今中国更是用5年时间,从无到有造出全球首台智能化数控机床,这也让中4家中标!中国移动全国通信工程设计大标12个专业公司开标从中国移动官网获悉,备受通信业界关注的,中国移动全国31个省份和15个专业公司年度通信工程设计大标已于日前陆续开标。截止目前,已有12家专业公司公示了中标结果,中移设计院广东电信研在旅途中拍摄风景和人像是1635F2。8实用还是2470F2。8实用?2470的焦段更合适。小广角到中长焦通吃了,旅行摄影非常合适,这个焦段也是我的挂机镜头焦段。刚刚从欧洲一个月的摄影归来,主要用的就是这个焦段。分享几张片子吧。首先,非常感谢你的信任苹果公司部分门店雇员拟组建工会,寻求更好待遇华尔街日报4月25日消息,美国一些最知名公司的员工在成立工会方面获得胜利后,苹果公司的零售人员正加紧行动,希望自己组建工会的努力也能取得成功。尽管美国工会会员人数近年来持续下降,但长安新能源奔奔EStar国民版25日停止收订来源中国质量新闻网余昶中国质量新闻网讯(余昶)4月24日凌晨,长安新能源官方微信公众号发布公告称,由于上游原材料短缺的影响及整车和零部件产能限制,导致该车型交付周期较长,将于4月2iPhone14硬件配置确定!升级了哪些地方?来了解一下苹果去年发布的iPhone13系列非常成功,全球各大市场销售依旧火爆,而今年秋季将要发布的iPhone14系列爆料消息频频,根据最新靠谱消息称,iPhone14系列即将迎来新配色,PN(多聚核苷酸)瑞吉明技术总监王超云博士新定义PN深海基因密码新定义PN(POLYNUCLEOTIDE)多聚核苷酸,众多核苷酸连成的链状聚合物,通过已研发的国内专利,从三文鱼精巢(精液)中提取并分离的高纯度的精确分子,作用和安苹果造车是跟风还是真的梦想?苹果优势芯片和系统,其实苹果造车的消息最早可以追溯到2013年,当时苹果CEO库克曾经表示,电动汽车同手机一样,只是一款电子产品,只不过体积更大而已。并且后来苹果还发布了泰坦计划,机身轻薄握感舒适,520送女友华为MateXs2折叠屏手机再好不过了一年一次的520情人节即将到来,应该有很多男士都在发愁送礼物的事情吧?如果你也为此十分烦恼,不如看看小编为大家推荐的礼物吧!它就是华为MateXs2折叠屏手机,这款手机拥有完善强大你的包裹由菜鸟守护,让更稳更快更好更绿成为可能面对疫情冲击,今年618大促的物流包裹能平稳送达吗?菜鸟给出的回答是肯定的。虽然现在疫情充满不确定性,但菜鸟一直在不确定性中寻找确定性,给商家和消费者更安心的守护。今日,菜鸟宣布启
提倡移动支付不等于拒现金据报道,网联清算平台数据显示,春节假期前五天,网联平台共处理跨机构网络支付交易62。36亿笔,金额4。20万亿元,同比增长5。30和11。58。其中,餐饮娱乐购物等是假期消费的主要达摩院口号喊得震天响,参保人数却暴露真相2017年前后,国内众多科技公司和互联网公司制定了全新的发展方向,尤其是阿里巴巴公司将目光瞄准芯片市场。在此期间,阿里集团不仅完成了对中天微的收购,而且还顺利成立达摩院。随着时间的抗白粉病与高产兼得我国小麦基因组编辑抗病育种取得突破新华社北京2月10日电(记者张泉)白粉病是危害小麦生产的重要病害,重病田减产可达40以上。我国科学家持续开展科研攻关,阐明了小麦新型mlo突变体既抗白粉病又高产的分子机制,并通过多商汤科技CEO徐立当AI能够替代天才的脑洞,人类能够识别出来吗?认知的创新突破,方能带来科研的创新发展纵观历史,大部分经济周期中非连续性的生产力跃迁,都来自于科研创新。同样,在当下的疫情全球经济问题和产业动能转换中,科研创新可以帮助我们有效穿越经济周期。我们这个时代见证了太多从科研LeetCode初中级算法题解排序算法介绍篇引言搜集题目的难度是在简单级别和中级级别,也是面试常考的题目。题目的题解,使用的开发语言是Swift。因为题目的描述很长,以及有各种案例提示,为了不占篇幅,所以没有展示出来,大家可新疆电子商务发展研究报告(2021)出炉新疆电子商务交易额同比增长17。1乌鲁木齐晚报全媒体讯(记者王丽丽)1月28日,新疆电子商务发展研究报告(2021)(以下简称报告)出炉。报告是由新疆大学经济与管理学院新疆数字经济研究院等(以下简称报告编制组)共同西藏5G终端用户数突破百万来源新华网新华社拉萨2月9日电(记者李键格桑边觉)记者从西藏自治区通信管理局获悉,西藏5G基站总数达6660个,基于5G技术的物联网已广泛应用于市政车联网智慧家庭等领域,5G终端用微信8。0。19正式版发布,又新增4个实用大技能,真是太给力了本文编辑今日头条作者小俊技术分享未经授权严禁转载,发现抄袭者将进行全网投诉分享生活小妙招,享受科技新生活!大家好,欢迎来到今天的知识分享!我是你们的好朋友小俊!微信在最近来说可以说运维工程师必会技能刀是杀人术,可刀鞘是仁心,有刀鞘的刀才是最锋利的。技术是杀人术,心态是仁心,有仁心的技术才是最厉害的。Linux1安装操作系统并了解系统的启动过程。2掌握基本的操作命令。3四剑客。法拉第未来携手韩国车企生产FF81第一财经汽车日评法拉第未来宣布与MyoungShin合作生产FF81据外媒报道,2月9日,法拉第未来(FaradayFuture,下称FF)宣布与韩国汽车制造商MyoungShin签约,后者将生产英特尔再杠台积两强抢单大战更激烈英特尔再杠台积两强抢单大战更激烈英特尔昨(8)日宣布,推出晶圆代工服务(IFS)加速器,打造全方位生态系联盟,重押硅智财(IP)与设计服务等领域,协助客户将概念实现为产品,同时将斥