C + + template metaprogramming-2 mimics the list of Haskell and related actions

Source: Internet
Author: User

This is what was written yesterday and today, using C + + variable template parameters package and package expansion, pattern matching some things do, feel with TypeName ... than the wheel uncle that list<a,list<b, List<c, d>>> design lot better a lot.

List has a very clever inheritance, only then can use similar to list<>::rest writing, directly defined as Template<typename T, TypeName ... Targs>list is not going to work.

Change here can give a args ... For another package, here to thank Lyp Chicory told me that the original template can continue to set the template, for my own blind groping Tml Road opened up another new world.

The principles of scanf and FOLDL are basically the same, but with my IQ it is impossible to unify them in a short time.

Printlist seems to use only functions to output it.

Write also found their own template meta-programming coding and naming style is constantly improving, sure enough to write to understand things.

(say the default font size how to change ah, 13px just look good)

1Template<typename t>2 structListget;3Template<typename ... Targs>4 structList;5 6Template<typename T, TypeName ... Targs>7 structListget<list<t, targs...>>8 {9     usingFirst =TypeName T;Ten     usingRest = list<targs...>; One }; A  -Template<typename ... Targs> - structList:listget<list<targs...>> the { -     usingBase = listget<list<targs...>>; -     usingSelf = list<targs...>; - }; +  -Template<typename t> + structListisempty; A  atTemplate<typename T, TypeName ... Targs> - structListisempty<list<t, targs...>> - { -     usingResult = bool<false>; - }; -Template<> in structListisempty<list<>> - { to     usingResult = bool<true>; + }; -  theTemplate<typename A, Template<typename ... Args> TypeName B> * structChange ; $ Panax NotoginsengTemplate<template<typename ... Args>typename A, Template<typename ... Args>typename B, TypeName ... Args> - structChange<a<args...>, b> the { +     usingResult = b<args...>; A }; the  +Template<typename T1, TypeName t2> - structListconnect; $  $Template<typename ... TARGS1, TypeName ... Targs2> - structListconnect<list<targs1...>, list<targs2...>> - { the     usingResult = TypeName list<targs1 ..., targs2...>; - };WuyiTemplate<typename t> the structListreverse; -  WuTemplate<typename ... Targs> - structListreverse<list<targs...>> About { $     usingParam = TypeName List<targs...>; -     usingResult = TypeName listconnect< -TypeName listreverse< - TypeName Param::rest A>:: Result, +TypeName List<typename param::first> the>:: Result; - }; $  theTemplate<typename t> the structListreverse<list<t>> the { the     usingResult = TypeName List<t>; - }; in  theTemplate<template<typename St> TypeName Tapplicative, TypeName t1> the structListmap; About  theTemplate<template<typename St> TypeName Tapplicative, TypeName ... Targs> the structListmap<tapplicative, list<targs...>> the { +     usingParam = TypeName List<targs...>; -     usingResult = TypeName listconnect< thelist<BayiTypeName tapplicative< the TypeName Param::first the>:: Result ->, -TypeName listmap< the tapplicative, the TypeName Param::rest the>:: Result the>:: Result; - }; the  theTemplate<template<typename St> TypeName Tapplicative, TypeName tlast> the structListmap<tapplicative, list<tlast>>94 { the     usingParam = TypeName List<tlast>; the     usingResult = TypeName List<typename tapplicative<tlast>::result>; the };98  AboutTemplate<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName t> - structListfoldleft;101 102Template<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName ... Targs>103 structListfoldleft<tval, Tbinfunc, list<targs...>>104 { the     usingParam = list<targs...>;106     usingResult = TypeName listfoldleft<107TypeName tbinfunc<108 Tval,109 TypeName Param::first the>:: Result,111 Tbinfunc, the TypeName Param::rest113>:: Result; the }; the  theTemplate<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName tlast>117 structListfoldleft<tval, Tbinfunc, list<tlast>>118 {119     usingParam = list<tlast>; -     usingResult = TypeName Tbinfunc<tval, TypeName param::first>:: Result;121 };122 123Template<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName t>124 structListscanleft; the 126Template<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName ... Targs>127 structListscanleft<tval, Tbinfunc, list<targs...>> - {129     usingParam = list<targs...>; the     usingValue = TypeName tbinfunc<131 Tval, the TypeName Param::first133>:: Result;134     usingResult = TypeName listconnect<135List<value>,136TypeName listscanleft<137 Value,138 Tbinfunc,139 TypeName Param::rest $>:: Result141>:: Result;142 };143 144Template<typename Tval, Template<typename TArg1, TypeName Targ2> TypeName Tbinfunc, TypeName tlast>145 structListscanleft<tval, Tbinfunc, list<tlast>>146 {147     usingParam = list<tlast>;148     usingValue = TypeName tbinfunc<149 Tval, Max TypeName Param::first151>:: Result; the     usingResult = list<value>;153 };154 155Template<typename T, T V, TypeName ... Trest>156 voidPrintlist (List<podtype<t, v>, trest...>)157 {158Std::cout << V <<' ';159Printlist (list<trest...>()); the }161 162Template<typename T, T v>163 voidPrintlist (List<podtype<t, v>>)164 {165Std::cout <<V;166}

(Sure enough, only the statically highlighted C + + code looks the same as Xiang)

That's how it's used.

1 listfoldleft<int<0;, Add, Decltype (list) >:: Result xxx; 2 listscanleft<int<3;, Add, Decltype (list) >:: Result yyy; 3 printlist (yyy); 4 printlist (XXX);

FOLDL1,FOLDR,FOLDR1 and the corresponding SCANR ... Ah ah ah ah ah oh ah

1 int Main () 2 {3     cout << l the whole person is feeling full of power!!!!!! "; 4     Main (); 5 }

C + + template metaprogramming-2 mimics the list of Haskell and related actions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.