C + + template metaprogramming-1 basic data types and operations

Source: Internet
Author: User
Tags mul

This is the blog opened a few days ago, C + + template has no way to save convenient floating point, forget it.

The basic type of design is referenced by the Vczh wheel tert-mimic boost for the MPL design.

The words template and TypeName write more is really infinite annoying ah, had to define a few macros, and then the definition of the operation is generated in bulk with a macro.

1Template<typename Tpod, Tpod v>2 structPodtype3 {4     Static ConstTpod Value =V;5     usingType =tpod;6     usingSelf = podtype<tpod, v>;7 };8 9template<intVint>Ten usingInt = podtype<int, vint>; One  Atemplate<BOOLVbool> - usingBool = podtype<BOOL, vbool>; -  thetemplate<CharVchar> - usingChar = podtype<Char, vchar>; -  - #defineTN TypeName + #defineTT Template - #defineSS struct +  A #defineDefine_bin_meta_operation_head (Name) atTt<tn T1, TN t2>                                 - SS Name -  - #defineDefine_bin_meta_operation_body (Name, Ttype, VType, Rtype, op) -Tt<vtype V1, VType v2>                                                     -SS Name<ttype<v1>, ttype<v2>>                                             in {                                                                         -     usingResult = TN rtype< (V1 op V2) >;  to } +  - #defineDefine_una_meta_operation_head (Name) theTt<tn t1>                                         * SS Name $ Panax Notoginseng #defineDefine_una_meta_operation_body (Name, Ttype, VType, Rtype, op) -Tt<vtype v1>                                                             theSS name<ttype<v1>>                                                         + {                                                                         A     usingResult = TN rtype< (op V1) >;  the } +  - Define_bin_meta_operation_head (ADD); $Define_bin_meta_operation_body (ADD, Int,int, Int, +); $Define_bin_meta_operation_body (ADD, Char,Char, Char, +); - Define_bin_meta_operation_head (Sub); -Define_bin_meta_operation_body (Sub, Int,int, Int,-); theDefine_bin_meta_operation_body (Sub, Char,Char, Char,-); - Define_bin_meta_operation_head (Mul);WuyiDefine_bin_meta_operation_body (Mul, Int,int, Int, *); theDefine_bin_meta_operation_body (Mul, Char,Char, Char, *); - Define_bin_meta_operation_head (DIV); WuDefine_bin_meta_operation_body (Div, Int,int, Int,/ ); -Define_bin_meta_operation_body (Div, Char,Char, Char,/ ); About Define_bin_meta_operation_head (Mod); $Define_bin_meta_operation_body (Mod, Int,int, Int,%); -Define_bin_meta_operation_body (Mod, Char,Char, Char,%); - Define_bin_meta_operation_head (and); -Define_bin_meta_operation_body (and, Bool,BOOL, Bool, &&); A Define_bin_meta_operation_head (Or); +Define_bin_meta_operation_body (Or, Bool,BOOL, Bool, | | ); the Define_bin_meta_operation_head (Xor); -Define_bin_meta_operation_body (Xor, Bool,BOOL, Bool, ^); $ Define_bin_meta_operation_head (Eq); theDefine_bin_meta_operation_body (Eq, Bool,BOOL, Bool, = = ); theDefine_bin_meta_operation_body (Eq, Int,int, Bool, = = ); theDefine_bin_meta_operation_body (Eq, Char,Char, Bool, = = ); the Define_bin_meta_operation_head (NEQ); -Define_bin_meta_operation_body (Neq, Bool,BOOL, Bool,! = ); inDefine_bin_meta_operation_body (Neq, Int,int, Bool,! = ); theDefine_bin_meta_operation_body (Neq, Char,Char, Bool,! = ); the Define_bin_meta_operation_head (Gt); AboutDefine_bin_meta_operation_body (Gt, Int,int, Bool, > ); theDefine_bin_meta_operation_body (Gt, Char,Char, Bool, > ); the Define_bin_meta_operation_head (Lt); theDefine_bin_meta_operation_body (Lt, Int,int, Bool, < ); +Define_bin_meta_operation_body (Lt, Char,Char, Bool, < ); - Define_bin_meta_operation_head (Ge); theDefine_bin_meta_operation_body (Ge, Int,int, Bool, >= );BayiDefine_bin_meta_operation_body (Ge, Char,Char, Bool, >= ); the Define_bin_meta_operation_head (Le); theDefine_bin_meta_operation_body (Le, Int,int, Bool, <= ); -Define_bin_meta_operation_body (Le, Char,Char, Bool, <= ); - Define_una_meta_operation_head (Neg); theDefine_una_meta_operation_body (Neg, Int,int, Int,-); theDefine_una_meta_operation_body (Neg, Char,Char, Char,-); the Define_una_meta_operation_head (not); theDefine_una_meta_operation_body (not, Bool,BOOL, Bool,!);

That's how it's used.

 1  int<1  > A;  2  int<2  >  b;  3  Add<decltype (a), Decltype (b) >::result Aplusb;  4  Sub<decltype (a), Decltype (b) >::result Aminusb;  5  false  > B1;  7  bool< B2;  8  And<decltype (B1), Decltype (B2) >::result b1andb2; 

The results of the calculations can be clearly seen from vs2015 IntelliSense.

That's it

C + + template metaprogramming-1 basic data types and operations

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.