Debian Codegraph @

yo/advancedtemplates/examples/exprtempl10.cc

raw
1 #include <iostream>
2 #include <vector>
3 #include <functional>
4
5 using namespace std;
6
7 enum
8 {
9 ADD,
10 SUB,
11 };
12
13 template<typename LHS, typename RHS, int operation>
14 struct BinExpr
15 {
16 template<typename Type>
17 struct BasicType;
18
19 template<typename LH, typename RH, int oper>
20 struct BasicType<BinExpr<LH, RH, oper>>;
21
22 using ObjType = typename BasicType<RHS>::ObjType;
23 using value_type = typename ObjType::value_type;
24
25
26 LHS const &d_lhs;
27 RHS const &d_rhs;
28
29 BinExpr(LHS const &lhs, RHS const &rhs)
30 :
31 d_lhs(lhs),
32 d_rhs(rhs)
33 {}
34
35 size_t size() const
36 {
37 return d_lhs.size();
38 }
39
40 template <typename LH, typename RH, int oper>
41 struct Operation;
42
43 value_type operator[](size_t ix) const
44 {
45 return Operation<LHS, RHS, operation>::cpt(d_lhs[ix], d_rhs[ix]);
46 // *************** this causes
47 // calling
48 // the proper
49 // cpt ****************
50 }
51
52 operator ObjType() const
53 {
54 ObjType retVal;
55
56 retVal.reserve(size());
57
58 for (size_t ix = 0, end = size(); ix != end; ++ix)
59 new(&retVal[ix]) value_type{ (*this)[ix] };
60
61 return retVal;
62 }
63 };
64
65 template<typename LHS, typename RHS, int operation>
66 template<typename Type>
67 struct BinExpr<LHS, RHS, operation>::BasicType
68 {
69 using ObjType = Type;
70 };
71
72 template<typename LHS, typename RHS, int operation>
73 template<typename LH, typename RH, int oper>
74 struct BinExpr<LHS, RHS, operation>::BasicType<BinExpr<LH, RH, oper>>
75 {
76 using ObjType = typename BinExpr<LH, RH, oper>::ObjType;
77 };
78
79 template<typename LHS, typename RHS, int operation>
80 template <typename LH, typename RH>
81 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, ADD>
82 {
83 static value_type cpt(value_type const &lhs, value_type const &rhs)
84 {
85 return lhs + rhs;
86 };
87 };
88
89 template<typename LHS, typename RHS, int operation>
90 template <typename LH, typename RH>
91 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, SUB>
92 {
93 static value_type cpt(value_type const &lhs, value_type const &rhs)
94 {
95 return lhs - rhs;
96 };
97 };
98
99 template<typename LHS, typename RHS>
100 BinExpr<LHS, RHS, ADD> operator+(LHS const &lhs, RHS const &rhs)
101 {
102 return BinExpr<LHS, RHS, ADD>(lhs, rhs);
103 }
104
105
106 template<typename LHS, typename RHS>
107 BinExpr<LHS, RHS, SUB> operator-(LHS const &lhs, RHS const &rhs)
108 {
109 return BinExpr<LHS, RHS, SUB>(lhs, rhs);
110 }
111
112
113
114 struct VI: public vector<int> // define the data structure to
115 { // operate on
116 static size_t const s_max = 1;//0000;
117
118 VI()
119 :
120 vector<int>(s_max, 1)
121 {}
122 };
123
124 int main()
125 {
126 // vector<int> one {1, 2, 3};
127 // vector<int> two {4, 5, 6};
128 //
129 // vector<int> three = one + two + (one + two);
130 //
131 // auto four = one + two + (one + two);
132 //
133 // auto z = "hello world";
134 //
135 // cout << z << '\n';
136 //
137 // cout << three[0] << ' ' << three[2] << '\n';
138 // cout << four[0] << ' ' << four[2] << '\n';
139
140 VI a, b, c, d;
141
142 d = a + b;
143
144 // d = a - b - c - d - (a - b - c - d);
145 //
146 // d = a + b - (c + d) - (a + b - c + d);
147 //
148 // d = VI{};
149 //
150 // d = a + b + c + d + (a + b + c + d);
151 //
152 cout << d.front() << ", " << d.back() << '\n';
153 }