Debian Codegraph @

yo/advancedtemplates/examples/exprtempl3.cc

raw
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <functional>
5
6 using namespace std;
7
8 template<typename LHS, typename RHS, template<typename> class Operation>
9 struct BinExpr;
10
11 template<typename Type>
12 struct BasicType
13 {
14 using ObjType = Type;
15 };
16
17 template<typename LHS, typename RHS, template<typename> class Operation>
18 struct BasicType<BinExpr<LHS, RHS, Operation>>
19 {
20 using ObjType = typename BinExpr<LHS, RHS, Operation>::ObjType;
21 };
22
23 template<typename LHS, typename RHS, template <typename> class Operation>
24 struct BinExpr
25 {
26 using ObjType = typename BasicType<RHS>::ObjType;
27 using value_type = typename ObjType::value_type;
28
29 LHS const &d_lhs;
30 RHS const &d_rhs;
31
32 BinExpr(LHS const &lhs, RHS const &rhs)
33 :
34 d_lhs(lhs),
35 d_rhs(rhs)
36 {}
37
38 size_t size() const
39 {
40 return d_lhs.size();
41 }
42
43 value_type operator[](size_t ix) const
44 {
45 static Operation<value_type> operation;
46
47 return operation(d_lhs[ix], d_rhs[ix]);
48 }
49
50 operator ObjType() const
51 {
52 ObjType retVal;
53
54 for (size_t ix = 0, end = size(); ix != end; ++ix)
55 retVal[ix] = (*this)[ix];
56
57 return retVal;
58 }
59 };
60
61 template<typename LHS, typename RHS>
62 BinExpr<LHS, RHS, plus> operator+(LHS const &lhs, RHS const &rhs)
63 {
64 return BinExpr<LHS, RHS, plus>(lhs, rhs);
65 }
66
67 struct VI: public vector<int>
68 {
69 static size_t const s_max = 10000;
70
71
72 VI()
73 :
74 vector<int>(s_max, 1)
75 {}
76 };
77
78 int main(int argc, char **argv)
79 {
80 if (argc == 1)
81 {
82 cout << "arg1: number of iterations\n";
83 return 0;
84 }
85
86 VI a, b, c, d;
87
88 for (size_t idx = 0, count = stoul(argv[1]); idx != count; ++idx)
89 d = a + b + c + d + a + b + c + d;
90
91 cout << d.front() << ", " << d.back() << '\n';
92 }