File: exprtempl11.cc

package info (click to toggle)
c%2B%2B-annotations 11.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,244 kB
  • sloc: cpp: 21,698; makefile: 1,505; ansic: 165; sh: 121; perl: 90
file content (172 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <vector>
#include <functional>

// See also addis.cc

using namespace std;

    enum
    {
        ADD,
        SUB,
        MUL
    };


template<typename LHS, typename RHS, int operation>
struct BinExpr
{
    template<typename Type>
    struct BasicType;

    template<typename LH, typename RH, int oper>
    struct BasicType<BinExpr<LH, RH, oper>>;

    typedef typename BasicType<RHS>::ObjType ObjType;
    typedef typename ObjType::value_type value_type;


    LHS const &d_lhs;
    RHS const &d_rhs;

    BinExpr(LHS const &lhs, RHS const &rhs)
    :
        d_lhs(lhs),
        d_rhs(rhs)
    {}

    size_t size() const
    {
        return d_lhs.size();
    }

    template <typename LH, typename RH, int oper>
    struct Operation;

    value_type operator[](size_t ix) const
    {
        return Operation<LHS, RHS, operation>::cpt(d_lhs[ix], d_rhs[ix]);
                // *************** this causes
                //                 calling
                //                 the proper
                //                 cpt          ****************
    }

    operator ObjType() const
    {
        ObjType retVal;

        retVal.reserve(size());

        for (size_t ix = 0, end = size(); ix != end; ++ix)
            new(&retVal[ix]) value_type{ (*this)[ix] };

        return retVal;
    }
};

template<typename LHS, typename RHS, int operation>
template<typename Type>
struct BinExpr<LHS, RHS, operation>::BasicType
{
    typedef Type ObjType;
};

template<typename LHS, typename RHS, int operation>
template<typename LH, typename RH, int oper>
struct BinExpr<LHS, RHS, operation>::BasicType<BinExpr<LH, RH, oper>>
{
    typedef typename BinExpr<LH, RH, oper>::ObjType ObjType;
};

template<typename LHS, typename RHS, int operation>
template <typename LH, typename RH>
struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, ADD>
{
    static value_type cpt(value_type const &lhs, value_type const &rhs)
    {
        return lhs + rhs;
    };
};

template<typename LHS, typename RHS, int operation>
template <typename LH, typename RH>
struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, SUB>
{
    static value_type cpt(value_type const &lhs, value_type const &rhs)
    {
        return lhs - rhs;
    };
};

template<typename LHS, typename RHS>
BinExpr<LHS, RHS, ADD>  operator+(LHS const &lhs, RHS const &rhs)
{
    return BinExpr<LHS, RHS, ADD>(lhs, rhs);
}


template<typename LHS, typename RHS>
BinExpr<LHS, RHS, SUB>  operator-(LHS const &lhs, RHS const &rhs)
{
    return BinExpr<LHS, RHS, SUB>(lhs, rhs);
}


struct VI                               // define the data structure to
{                                       // operate on
    static size_t s_count;
    typedef int value_type;

    VI() = default;

    VI(VI const &other)
    {
        (*this)[0] = other[0];
        cout << "CC, count = " << s_count << '\n';
    }

    VI(VI &&tmp)
    {
        (*this)[0] = tmp[0];
        cout << "MC, count = " << s_count << '\n';
    }

    int d_value = 0;

    size_t size() const
    {
        return 1;
    };

    void reserve(size_t count)
    {};

    int &operator[](size_t idx)
    {
        ++s_count;
        return d_value;
    }

    int const &operator[](size_t idx) const
    {
        ++s_count;
        return d_value;
    }
};

size_t VI::s_count;


int main()
{
    VI a, b, c, d;

    VI e = a + b  + c + d;
//    (a + b + c + d).operator VI();  // the resulting sum is computed once
                                    // the conversion operator is called.
//    static_cast<VI>(a + b + c + d);

    cout << "# index computations: " << VI::s_count << '\n';
}