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

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; }

iPhone13双十一会降价吗?不会降价,双十一的主力军是iPhone12系列,iPhone13系列现在看来,如果能供货充足,按时发货就很好了。或许会出现一种情况,iPhone12系列降价幅度大,iPhone13GalaxyA50开始获取Android11更新在向高端和各种中端智能手机推送Android11更新后,三星已开始将Android11更新推送到GalaxyA50。除了OneUI3。1,最新更新还带来了2021年3月的安全补丁。这家公司为什么这么火?创业邦2016年,阿里在国内率先提出中台战略。同年,包志刚成立云徙科技(以下简称云徙科技),并作为阿里的核心生态合作伙伴参与阿里中台第一个对外输出的企业项目。成立三年来,云徙发力于快消地新形势下,实战专家支招营销突围战今年的春节,突如其来的黑天鹅让无数企业遭遇不小的冲击。危机中的企业面临严峻挑战,营销从线下转到线上,成为企业自救的关键。那么,在数智时代,企业如何利用线上数据促进业务创新?数智如何云徙上海分部正式启动,增速引航长三角市场徙讯导读进驻上海,对于云徙在华东地区的发展,将是一个新起点,一次新势能,一场新范式革命。2019年12月2日,云徙科技正式进驻上海,上海新办公室今日启幕,投入使用。新上海办公室选址在虹国产中型车能月销过万,13万左右就能买到,红旗H5的高光时刻虽说国产品牌在SUV市场已经很有话语权,但在轿车领域却没什么大的作为,依旧被合资品牌把控,首先国产中型车本就比较少,其次就是大部分国产品牌的影响力不足以支撑起中型车的价格,唯一月销一直活在雅阁的阴影里,只怪本田INSPIRE的名字太难记本田INSPIRE跟雅阁是兄弟车系,是本田在中国市场祭出的双车战略,但相比雅阁月均近两万辆的销量来说,INSPIRE的销量就很尴尬了,月均销量只有四五千辆,可以说一直活在雅阁阴影当起售价19。9888万,大众ID。4X这定价你服了吗?不管你愿不愿意,纯电动汽车都在一点点蚕食着传统燃油车的市场份额,这一场汽车革命始于那些造车新势力,而大多数传统车企都是后知后觉,但其实它们也在逐渐调整产品策略,上汽大众就推出了首款仅29。88万的进口中大型SUV,标配3。5LV6发动机这两年中大型SUV市场的竞争非常激烈,大众途昂雪佛兰开拓者别克昂科旗福特探险者都各具特色,今年现代也按捺不住那颗骚动的心了,也想搅搅局,将帕里斯帝引进到了国内,全系标配3。5LV6畅销20年,曾经的驾驶者之车沦为买菜车,宝来堪称大众的名片2001年,一汽大众推出了全新三厢轿车宝来,在当时凭借动感的造型以及领先的技术,被誉为驾驶者之车,当时的宝来算是一款十分高级的轿车,那个年代能买得起宝来的家庭,绝对算是提前步入小康小号汉EV来了!12。98万起售,比亚迪秦PLUSEV上市比亚迪是国内最早进行新能源汽车研发的自主车企,目前已经发展成为国内乃至世界新能源汽车的引领者,比亚迪汉系列成功登陆欧洲,而今年秦PLUS系列又大发异彩,早些时候发布的秦PLUSDM
2021工资表(全自动生成)。xls薪酬HR的两项核心工作算工资和设计工资体系算工资讲究高效准确工资设计讲究科学合理哪一项工作都是费时费力今天小编的免费福利是2021工资表与工资方案资料表1套带函数公式的工资表系统5在电动汽车时代的影响下,未来的道路指示牌会是这样?长期以来,特斯拉的自动驾驶模式就像传统汽车制造商无法忍受的痒。如今,特斯拉连续四个获利季度(其股价徘徊在1600美元左右)之后,在电动汽车领域处于领导地位。如今,这家硅谷公司是全球电动汽车的未来是哪一个?氢能源还是电池汽车电池驱动的电动汽车(BEV)在逐步向绿色交通方式发展的过程中逐渐取代了内燃机。替代方案是氢能汽车或燃料电池电动汽车(FCEV)。两者均由电动机驱动,但如今电动汽车大部分由锂离子电池蔚来终身免费换电截止到今日24时前,您怎么看?近日,蔚来官方宣称,蔚来将对10月12日0点后支付大定金的用户,调整换电权益,不再享受老车主的无限次免费换电权益,改为每月前6次换电仅收取电费,免收服务费放弃专属桩权益的用户每月前国产Model3出口欧洲,中国有望成为全球电动车制造中心吗?10月26日,特斯拉上海超级工厂中国制造整车出口欧洲仪式在位于上海临港的特斯拉上海超级工厂举行。约7000台中国制造的Model3整车将于明天启程远航,出口到欧洲市场,预计下月底到各国纷纷尝试禁售燃油车,你觉得全面禁燃离我们还远吗?美国加利福尼亚州是电动汽车的坚定支持者,该地区有许多针对电动汽车销售的利好政策,并且加州也制定了多项措施来减少来自燃油车尾气排放造成的污染。此前据外媒报道,美国加州州长加文纽森(G特斯拉新电池成本下降,续航增加16,将供应电动车全行业在特斯拉于9月22日星期二举行的电池日活动之前,首席执行官埃隆马斯克周一在Twitter上解释说,尽管特斯拉一直计划增加从松下LGCATL以及其他合作伙伴的上面增加电池购买量,但预大众汽车正在加大设备投资,以期成为电动汽车的市场领导者大众汽车正在工厂投资额外的自动化设备,以制造ID。4电动跨界车和遵循IDBuzz电动货车概念的生产模型两种销往美国的模型。大众汽车在周三的新闻稿中表示,已为田纳西州查塔努加和德国埃电动汽车的到来速度比预期的要快比尔盖茨(BillGates)说创新发展的速度非常快。在电动汽车领域,这是最真实的。电动汽车的到来速度比许多业内人士早在5或10年前所预期的要快得多。汽车制造商正在醒来。他们并不孤沃尔沃首款纯电动汽车待上线,价格可与特斯拉ModelY媲美尔沃周三宣布了XC40Recharge电动跨界车在美国的定价,这是它的第一款大众市场电动车。XC40Recharge的起价为54,895(包括强制性目的地收费),但有资格获得全部7新电池技术让充电和加油一样快,你会选择电动车吗?里程焦虑和充电焦虑与新能源车如影随形,也一定程度阻碍了它的发展。说起人们不想购买新能源车的原因,无非就是怕电量不够半路熄火,还有里程太少上不了高速,总的来说里程焦虑和充电焦虑确实是