C + + template metaprogramming-3 logical structure, recursion, a little list of bits and pieces, a little Sfinae

Source: Internet
Author: User

Originally want to put scanr,foldr what all wrote, a think too troublesome, even, template meta-programming almost also the end of, leave study still have 10 days, before a few days department also want to naxin what, can't write a few days code, so hurriedly put this end off, tomorrow continue to copy Wheel uncle's Win32 library go.

Logical structure and recursion white is to do an if, a for_n,if and excel in the same as if, for_n is a template structure iteration N times, in order to so-called convenience, the If made a macro, write it kinda interesting

1Template<typename TTest, TypeName Ttrue, TypeName tfalse>2 structIf;3 4Template<typename Ttrue, TypeName tfalse>5 structif<bool<true, Ttrue, tfalse>6 {7     usingResult =ttrue;8 };9 TenTemplate<typename Ttrue, TypeName tfalse> One structif<bool<false, Ttrue, tfalse> A { -     usingResult =Tfalse; - }; the  -Template<typename Ttimes, Template<typename tresult> TypeName Tproc, TypeName tdefval> - structFor_n; -  +Template<template<typename Tresult> TypeName Tproc, TypeName Tdefval,intVtimes> - structFor_n<int<vtimes&gt, Tproc, tdefval> + { A     usingResult = TypeName tproc< atTypeName for_n< -Int<vtimes-1>, - Tproc, - Tdefval ->:: Result ->:: Result; in }; -  toTemplate<template<typename Tresult> TypeName Tproc, TypeName tdefval> + structfor_n<int<0, Tproc, tdefval> - { the     usingResult =TypeName Tdefval; * }; $ Panax Notoginseng #ifDefined simplified_tml -  the #defineIF if< + #defineThen, A #defineELSE, the #defineNoelse, void + #defineNothen, void - #defineENDIF >::result $ #defineELIF ELSE IF $  - #endif

The usage is not given, the unit test is not written

Then there is a part of the list that is fragmented, and filter is filtering the entire list according to a given unary predicate, and Mapnone and Filternone belong to the default action of Nothing/Direct return true, Listgeneration is to generate the list from the given list, Just like the list derivation of Haskell, White is a filter and then a map, the combination is very interesting, range is given a type and upper and lower bounds, generate a continuous interval, here suddenly thought can also be customized step, too lazy to change it.

1Template<template<typename Targ> TypeName Tfilter, TypeName t>2 structListfilter;3 4Template<template<typename Targ> TypeName Tfilter, TypeName ... Targs>5 structListfilter<tfilter, list<targs...>>6 {7     usingParam = list<targs...>;8     usingResult = TypeName listconnect<9TypeName if<TenTypeName Tfilter<typename param::first>:: Result, OneList<typename param::first>, AList<> ->:: Result, -TypeName listfilter< the Tfilter, - TypeName Param::rest ->:: Result ->:: Result; + }; -  +Template<template<typename Targ> TypeName Tfilter, TypeName tlast> A structListfilter<tfilter, list<tlast>> at { -     usingParam = list<tlast>; -     usingResult = TypeName if< -TypeName Tfilter<typename param::first>:: Result, -List<typename param::first>, -List<> in>:: Result; - }; to  +Template<typename t> - structMapnone the { *     usingResult =T; $ };Panax Notoginseng  -Template<typename t> the structFilternone + { A     usingResult = bool<true>; the }; +  -Template<template<typename tval> TypeName Tproctoval, Template<typename tval> TypeName TFilterProc, TypeName ... Tseq> $ structlistgeneration; $  -Template<template<typename tval> TypeName Tproctoval, Template<typename tval> TypeName TFilterProc, TypeName ... Tseq> - structListgeneration<tproctoval, Tfilterproc, list<tseq...>> the { -     usingFilterresult = TypeName Listfilter<tfilterproc, list<tseq...>>:: Result;Wuyi     usingResult = TypeName Listmap<tproctoval, filterresult>:: Result; the }; -  WuTemplate<typename Ttype, Ttype vbegin, Ttype vend, ttype Vcurr = vbegin> - structRange About { $     usingResult = TypeName listconnect< -List<podtype<ttype, vcurr>>,  -TypeName if< -Bool<vcurr < vend>,  ATypeName Range<ttype, Vbegin, vend, Vcurr +1>:: Result, +List<> the>:: Result ->:: Result; $};

It's pretty cool to use.

1 listgeneration<mul2, filternone, range<int0>::result>::result List2;

MUL2 is a well-defined operation, that is, to accept a type value, and then multiply it by the number of recursive layers of the 2,c++ template can not exceed 255 the original refers to the entire expression of the recursive layer, ah, I have the upper bound to write up to 63 can not write down.

SFINAE, template substitution failure is not an error, when the compiler looks for overloaded functions based on the template, if an overloaded template substitution fails, then the compiler will look for additional overloads until the final error is not found, so you can overload a filter with a set of functions, Different return values are performed according to different template parameter executions, and the purpose of type filtering and error checking is achieved by returning values.

C++17 's concept is also based on Sfinae, good excitement.

Only look at the type sfinae,expression haven't seen, also not ready to see, feel no eggs, or over time to buy the C + + template meta-programming system to learn about it.

Here is a translation of the things on cppreference.com:

The following are SFINAE errors

  • Attempt to instantiate a package extension that contains a number of different length packages (not understood, what does that mean)
  • Attempt to create an array of void arrays, reference arrays, arrays of functions, arrays of abstract classes, negative numbers, or 0-length arrays
  • Attempt to use a non-class or enum type on the left side of the: operator
  • Try to use a member of a type if: try to create a pointer to a reference
    • The type does not contain this member
    • Where a type is required, this member is not a type
    • Where templates are required, this member is not a template type
    • Where non-types are required, this member is not a non-type
  • Attempt to create a void reference
  • An attempt was made to create a pointer to a member of T, but T is not a class type
  • A value that attempts to give an error type to a non-type template parameter
  • An attempt was made to make an invalid conversion in the following scenario: attempting to create a function with a parameter of type void
    • In the template parameter expression
    • In a function declaration expression
  • Try to create a function that returns a function or an array
  • Try to create a const volatile modifier function
  • An attempt to create a parameter or return value is a function of an abstract class

I did not write anything, not very familiar with the path, and so I have C + + those strange syntax and pointers to clarify what is here to be almost.

C + + template metaprogramming-3 logical structure, recursion, a little list of bits and pieces, a little Sfinae

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.