• C++模板元编程


    这是博客开通前几天做的,C++的模板没办法存方便的浮点数,算了。

    基本类型的设计参考了vczh轮子叔模仿boost的MPL的设计。

    话说template和typename写多了真是无限烦人啊,不得已定义了几个宏,然后运算的定义也是用宏批量生成的。

     1 template<typename TPOD, TPOD V>
     2 struct PODType
     3 {
     4     static const TPOD Value = V;
     5     using Type = TPOD;
     6     using Self = PODType<TPOD, V>;
     7 };
     8 
     9 template<int VInt>
    10 using Int = PODType<int, VInt>;
    11 
    12 template<bool VBool>
    13 using Bool = PODType<bool, VBool>;
    14 
    15 template<char VChar>
    16 using Char = PODType<char, VChar>;
    17 
    18 #define TN typename
    19 #define TT template
    20 #define SS struct
    21 
    22 #define DEFINE_BIN_META_OPERATION_HEAD(Name)    
    23 TT<TN T1, TN T2>                                
    24 SS Name
    25 
    26 #define DEFINE_BIN_META_OPERATION_BODY(Name, TType, VType, RType, op)    
    27 TT<VType V1, VType V2>                                                    
    28 SS Name<TType<V1>, TType<V2>>                                            
    29 {                                                                        
    30     using Result = TN RType<(V1 op V2)>;                                
    31 }
    32 
    33 #define DEFINE_UNA_META_OPERATION_HEAD(Name)    
    34 TT<TN T1>                                        
    35 SS Name
    36 
    37 #define DEFINE_UNA_META_OPERATION_BODY(Name, TType, VType, RType, op)    
    38 TT<VType V1>                                                            
    39 SS Name<TType<V1>>                                                        
    40 {                                                                        
    41     using Result = TN RType<(op V1)>;                                    
    42 }
    43 
    44 DEFINE_BIN_META_OPERATION_HEAD(Add);
    45 DEFINE_BIN_META_OPERATION_BODY(Add, Int, int, Int, +);
    46 DEFINE_BIN_META_OPERATION_BODY(Add, Char, char, Char, +);
    47 DEFINE_BIN_META_OPERATION_HEAD(Sub);
    48 DEFINE_BIN_META_OPERATION_BODY(Sub, Int, int, Int, -);
    49 DEFINE_BIN_META_OPERATION_BODY(Sub, Char, char, Char, -);
    50 DEFINE_BIN_META_OPERATION_HEAD(Mul);
    51 DEFINE_BIN_META_OPERATION_BODY(Mul, Int, int, Int, *);
    52 DEFINE_BIN_META_OPERATION_BODY(Mul, Char, char, Char, *);
    53 DEFINE_BIN_META_OPERATION_HEAD(Div);
    54 DEFINE_BIN_META_OPERATION_BODY(Div, Int, int, Int, / );
    55 DEFINE_BIN_META_OPERATION_BODY(Div, Char, char, Char, / );
    56 DEFINE_BIN_META_OPERATION_HEAD(Mod);
    57 DEFINE_BIN_META_OPERATION_BODY(Mod, Int, int, Int, %);
    58 DEFINE_BIN_META_OPERATION_BODY(Mod, Char, char, Char, %);
    59 DEFINE_BIN_META_OPERATION_HEAD(And);
    60 DEFINE_BIN_META_OPERATION_BODY(And, Bool, bool, Bool, &&);
    61 DEFINE_BIN_META_OPERATION_HEAD(Or);
    62 DEFINE_BIN_META_OPERATION_BODY(Or, Bool, bool, Bool, || );
    63 DEFINE_BIN_META_OPERATION_HEAD(Xor);
    64 DEFINE_BIN_META_OPERATION_BODY(Xor, Bool, bool, Bool, ^);
    65 DEFINE_BIN_META_OPERATION_HEAD(Eq);
    66 DEFINE_BIN_META_OPERATION_BODY(Eq, Bool, bool, Bool, == );
    67 DEFINE_BIN_META_OPERATION_BODY(Eq, Int, int, Bool, == );
    68 DEFINE_BIN_META_OPERATION_BODY(Eq, Char, char, Bool, == );
    69 DEFINE_BIN_META_OPERATION_HEAD(Neq);
    70 DEFINE_BIN_META_OPERATION_BODY(Neq, Bool, bool, Bool, != );
    71 DEFINE_BIN_META_OPERATION_BODY(Neq, Int, int, Bool, != );
    72 DEFINE_BIN_META_OPERATION_BODY(Neq, Char, char, Bool, != );
    73 DEFINE_BIN_META_OPERATION_HEAD(Gt);
    74 DEFINE_BIN_META_OPERATION_BODY(Gt, Int, int, Bool, > );
    75 DEFINE_BIN_META_OPERATION_BODY(Gt, Char, char, Bool, > );
    76 DEFINE_BIN_META_OPERATION_HEAD(Lt);
    77 DEFINE_BIN_META_OPERATION_BODY(Lt, Int, int, Bool, < );
    78 DEFINE_BIN_META_OPERATION_BODY(Lt, Char, char, Bool, < );
    79 DEFINE_BIN_META_OPERATION_HEAD(Ge);
    80 DEFINE_BIN_META_OPERATION_BODY(Ge, Int, int, Bool, >= );
    81 DEFINE_BIN_META_OPERATION_BODY(Ge, Char, char, Bool, >= );
    82 DEFINE_BIN_META_OPERATION_HEAD(Le);
    83 DEFINE_BIN_META_OPERATION_BODY(Le, Int, int, Bool, <= );
    84 DEFINE_BIN_META_OPERATION_BODY(Le, Char, char, Bool, <= );
    85 DEFINE_UNA_META_OPERATION_HEAD(Neg);
    86 DEFINE_UNA_META_OPERATION_BODY(Neg, Int, int, Int, -);
    87 DEFINE_UNA_META_OPERATION_BODY(Neg, Char, char, Char, -);
    88 DEFINE_UNA_META_OPERATION_HEAD(Not);
    89 DEFINE_UNA_META_OPERATION_BODY(Not, Bool, bool, Bool, !);

    使用起来是这样的

    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 
    6 Bool<false> b1;
    7 Bool<true> b2;
    8 And<decltype(b1), decltype(b2)>::Result b1andb2;

    可以从vs2015的intellisense很清楚得看到运算结果

    就这样

  • 相关阅读:
    [uboot] (番外篇)uboot relocation介绍(转)
    [uboot] (番外篇)global_data介绍(转)
    [uboot] (第三章)uboot流程——uboot-spl代码流程 后续2018版本分析
    AddressUtils
    ruoyi HttpUtils
    ruoyi IpUtils
    ruoyi StringUtils
    JSONObject
    jackson解析处理JSON
    spring boot pom demo
  • 原文地址:https://www.cnblogs.com/pointer-smq/p/4779226.html
Copyright © 2020-2023  润新知