-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP532.HybridMetaprogramming.cpp
More file actions
46 lines (42 loc) · 978 Bytes
/
P532.HybridMetaprogramming.cpp
File metadata and controls
46 lines (42 loc) · 978 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
#include <iostream>
#include <type_traits>
#include <array>
#include <cassert>
template<typename T, std::size_t N>
auto dotProduct(const std::array<T, N>& x, const std::array<T, N>& y)
{
T result{};
for (std::size_t i = 0; i < N; ++i)
{
result += x[i] * y[i];
}
return result;
}
template<typename T, std::size_t N>
struct DotProduct
{
static inline T result(const T* a, const T* b)
{
return *a * *b + DotProduct<T, N-1>::result(a+1, b+1);
}
};
template<typename T>
struct DotProduct<T, 0>
{
static inline T result(const T*, const T*)
{
return T{};
}
};
template<typename T, std::size_t N>
auto dotProduct2(const std::array<T, N>& x, const std::array<T, N>& y)
{
return DotProduct<T, N>::result(x.begin(), y.begin());
}
int main(int argc, char const *argv[])
{
std::array<int, 3> arr1 = {1, 2, 3};
auto arr2 = arr1;
assert(dotProduct(arr1, arr2) == dotProduct2(arr1, arr2));
return 0;
}