Debian Codegraph @

yo/advancedtemplates/examples/exprtempl11.cc

raw
1 #include <iostream>
2 #include <vector>
3 #include <functional>
4
5 // See also addis.cc
6
7 using namespace std;
8
9 enum
10 {
11 ADD,
12 SUB,
13 MUL
14 };
15
16
17 template<typename LHS, typename RHS, int operation>
18 struct BinExpr
19 {
20 template<typename Type> // -> 69
21 struct BasicType;
22
23 template<typename LH, typename RH, int oper> // -> 76
24 struct BasicType<BinExpr<LH, RH, oper>>;
25
26 using ObjType = typename BasicType<RHS>::ObjType; // LHS, RHS must
27 using value_type = typename ObjType::value_type; // define a value_type
28
29
30 LHS const &d_lhs;
31 RHS const &d_rhs;
32
33 BinExpr(LHS const &lhs, RHS const &rhs) // std constructor
34 : // sets the refs
35 d_lhs(lhs),
36 d_rhs(rhs)
37 {}
38
39 size_t size() const // LHS, RHS must define
40 { // size: the size of the
41 return d_lhs.size(); // orig. LHS type
42 } // (RHS size is also OK)
43
44 template <typename LH, typename RH, int oper>
45 struct Operation;
46
47 value_type operator[](size_t ix) const
48 {
49 return Operation<LHS, RHS, operation>::cpt(d_lhs[ix], d_rhs[ix]);
50 // *************** this causes
51 // calling
52 // the proper
53 // cpt ****************
54 }
55
56 operator ObjType() const
57 {
58 ObjType retVal;
59
60 retVal.reserve(size());
61
62 for (size_t ix = 0, end = size(); ix != end; ++ix)
63 new(&retVal[ix]) value_type{ (*this)[ix] };
64
65 return retVal;
66 }
67 };
68
69 template<typename LHS, typename RHS, int operation> // 69
70 template<typename Type>
71 struct BinExpr<LHS, RHS, operation>::BasicType
72 {
73 using ObjType = Type;
74 };
75
76 template<typename LHS, typename RHS, int operation> // 76
77 template<typename LH, typename RH, int oper>
78 struct BinExpr<LHS, RHS, operation>::BasicType<BinExpr<LH, RH, oper>>
79 {
80 using ObjType = typename BinExpr<LH, RH, oper>::ObjType;
81 };
82
83 template<typename LHS, typename RHS, int operation>
84 template <typename LH, typename RH>
85 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, ADD>
86 {
87 static value_type cpt(value_type const &lhs, value_type const &rhs)
88 {
89 return lhs + rhs;
90 };
91 };
92
93 template<typename LHS, typename RHS, int operation>
94 template <typename LH, typename RH>
95 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, SUB>
96 {
97 static value_type cpt(value_type const &lhs, value_type const &rhs)
98 {
99 return lhs - rhs;
100 };
101 };
102
103 template<typename LHS, typename RHS>
104 BinExpr<LHS, RHS, ADD> operator+(LHS const &lhs, RHS const &rhs)
105 {
106 return BinExpr<LHS, RHS, ADD>(lhs, rhs);
107 }
108
109
110 template<typename LHS, typename RHS>
111 BinExpr<LHS, RHS, SUB> operator-(LHS const &lhs, RHS const &rhs)
112 {
113 return BinExpr<LHS, RHS, SUB>(lhs, rhs);
114 }
115
116
117 struct VI // define the data structure to
118 { // operate on
119 static size_t s_count;
120 using value_type = int;
121
122 VI() = default;
123
124 VI(VI const &other)
125 {
126 (*this)[0] = other[0];
127 cout << "CC, count = " << s_count << '\n';
128 }
129
130 VI(VI &&tmp)
131 {
132 (*this)[0] = tmp[0];
133 cout << "MC, count = " << s_count << '\n';
134 }
135
136 int d_value = 0;
137
138 size_t size() const
139 {
140 return 1;
141 };
142
143 void reserve(size_t count)
144 {};
145
146 int &operator[](size_t idx)
147 {
148 ++s_count;
149 return d_value;
150 }
151
152 int const &operator[](size_t idx) const
153 {
154 ++s_count;
155 return d_value;
156 }
157 };
158
159 size_t VI::s_count;
160
161
162 int main()
163 {
164 VI a, b, c, d;
165
166 VI e = a + b + c + d;
167 // (a + b + c + d).operator VI(); // the resulting sum is computed once
168 // the conversion operator is called.
169 // static_cast<VI>(a + b + c + d);
170
171 // 2nd cout avoids the 'not used' compiler warning
172 // cout << "# index computations: " << VI::s_count << '\n';
173 cout << "# index computations: " << e.s_count << '\n';
174 }