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