-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP385.Accumulate.cpp
More file actions
54 lines (51 loc) · 1007 Bytes
/
P385.Accumulate.cpp
File metadata and controls
54 lines (51 loc) · 1007 Bytes
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
#include <iostream>
template<typename T>
struct AccumulationTraits;
template<>
struct AccumulationTraits<char>
{
using AccT = int;
static constexpr AccT zero = 0;
};
template<>
struct AccumulationTraits<short>
{
using AccT = int;
static constexpr AccT zero = 0;
};
template<>
struct AccumulationTraits<int>
{
using AccT = long;
static constexpr AccT zero = 0;
};
template<>
struct AccumulationTraits<unsigned int>
{
using AccT = unsigned long;
static constexpr AccT zero = 0;
};
template<>
struct AccumulationTraits<float>
{
using AccT = double;
static constexpr AccT zero = 0;
};
template<typename T, typename AT = AccumulationTraits<T>>
auto accum(const T* beg, const T* end)
{
using AccT = typename AT::AccT;
AccT res = AT::zero;
while (beg != end)
{
res += *beg;
beg++;
}
return res;
}
int main(int argc, char const *argv[])
{
int arr[3] = {1, 2, 3};
std::cout << accum(arr, arr+3) << std::endl;
return 0;
}