C++模板元编程 - 1 基本数据类型和运算

时间:2022-07-14 02:38:01

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

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

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

 template<typename TPOD, TPOD V>
struct PODType
{
static const TPOD Value = V;
using Type = TPOD;
using Self = PODType<TPOD, V>;
}; template<int VInt>
using Int = PODType<int, VInt>; template<bool VBool>
using Bool = PODType<bool, VBool>; template<char VChar>
using Char = PODType<char, VChar>; #define TN typename
#define TT template
#define SS struct #define DEFINE_BIN_META_OPERATION_HEAD(Name) \
TT<TN T1, TN T2> \
SS Name #define DEFINE_BIN_META_OPERATION_BODY(Name, TType, VType, RType, op) \
TT<VType V1, VType V2> \
SS Name<TType<V1>, TType<V2>> \
{ \
using Result = TN RType<(V1 op V2)>; \
} #define DEFINE_UNA_META_OPERATION_HEAD(Name) \
TT<TN T1> \
SS Name #define DEFINE_UNA_META_OPERATION_BODY(Name, TType, VType, RType, op) \
TT<VType V1> \
SS Name<TType<V1>> \
{ \
using Result = TN RType<(op V1)>; \
} 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, -);
DEFINE_BIN_META_OPERATION_BODY(Sub, Char, char, Char, -);
DEFINE_BIN_META_OPERATION_HEAD(Mul);
DEFINE_BIN_META_OPERATION_BODY(Mul, Int, int, Int, *);
DEFINE_BIN_META_OPERATION_BODY(Mul, Char, char, Char, *);
DEFINE_BIN_META_OPERATION_HEAD(Div);
DEFINE_BIN_META_OPERATION_BODY(Div, Int, int, Int, / );
DEFINE_BIN_META_OPERATION_BODY(Div, Char, char, Char, / );
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, &&);
DEFINE_BIN_META_OPERATION_HEAD(Or);
DEFINE_BIN_META_OPERATION_BODY(Or, Bool, bool, Bool, || );
DEFINE_BIN_META_OPERATION_HEAD(Xor);
DEFINE_BIN_META_OPERATION_BODY(Xor, Bool, bool, Bool, ^);
DEFINE_BIN_META_OPERATION_HEAD(Eq);
DEFINE_BIN_META_OPERATION_BODY(Eq, Bool, bool, Bool, == );
DEFINE_BIN_META_OPERATION_BODY(Eq, Int, int, Bool, == );
DEFINE_BIN_META_OPERATION_BODY(Eq, Char, char, Bool, == );
DEFINE_BIN_META_OPERATION_HEAD(Neq);
DEFINE_BIN_META_OPERATION_BODY(Neq, Bool, bool, Bool, != );
DEFINE_BIN_META_OPERATION_BODY(Neq, Int, int, Bool, != );
DEFINE_BIN_META_OPERATION_BODY(Neq, Char, char, Bool, != );
DEFINE_BIN_META_OPERATION_HEAD(Gt);
DEFINE_BIN_META_OPERATION_BODY(Gt, Int, int, Bool, > );
DEFINE_BIN_META_OPERATION_BODY(Gt, Char, char, Bool, > );
DEFINE_BIN_META_OPERATION_HEAD(Lt);
DEFINE_BIN_META_OPERATION_BODY(Lt, Int, int, Bool, < );
DEFINE_BIN_META_OPERATION_BODY(Lt, Char, char, Bool, < );
DEFINE_BIN_META_OPERATION_HEAD(Ge);
DEFINE_BIN_META_OPERATION_BODY(Ge, Int, int, Bool, >= );
DEFINE_BIN_META_OPERATION_BODY(Ge, Char, char, Bool, >= );
DEFINE_BIN_META_OPERATION_HEAD(Le);
DEFINE_BIN_META_OPERATION_BODY(Le, Int, int, Bool, <= );
DEFINE_BIN_META_OPERATION_BODY(Le, Char, char, Bool, <= );
DEFINE_UNA_META_OPERATION_HEAD(Neg);
DEFINE_UNA_META_OPERATION_BODY(Neg, Int, int, Int, -);
DEFINE_UNA_META_OPERATION_BODY(Neg, Char, char, Char, -);
DEFINE_UNA_META_OPERATION_HEAD(Not);
DEFINE_UNA_META_OPERATION_BODY(Not, Bool, bool, Bool, !);

使用起来是这样的

 Int<> a;
Int<> b;
Add<decltype(a), decltype(b)>::Result aplusb;
Sub<decltype(a), decltype(b)>::Result aminusb; Bool<false> b1;
Bool<true> b2;
And<decltype(b1), decltype(b2)>::Result b1andb2;

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

C++模板元编程 - 1 基本数据类型和运算

就这样