1 | template <class T, int N> |
---|
2 | struct static_poly { |
---|
3 | T m_data[N]; //constexpr std::array isn't modifiable in C++14 (P0107R0) |
---|
4 | |
---|
5 | // construct: |
---|
6 | constexpr static_poly() : m_data{} {} |
---|
7 | |
---|
8 | constexpr T& operator[] (int i) { |
---|
9 | return m_data[i]; |
---|
10 | } |
---|
11 | |
---|
12 | constexpr const T& operator[] (int i) const { |
---|
13 | return m_data[i]; |
---|
14 | } |
---|
15 | }; |
---|
16 | |
---|
17 | template <class T, int N1, int N2> |
---|
18 | constexpr static_poly<T, N1 + N2> operator * (const static_poly<T, N1>& a, const static_poly<T, N2>& b) { |
---|
19 | static_poly<T, N1 + N2> prod; |
---|
20 | for (int i = 0; i < N1; ++i) |
---|
21 | for (int j = 0; j < N2; ++j) |
---|
22 | prod[i+j] += a[i] * b[j]; |
---|
23 | return prod; |
---|
24 | } |
---|
25 | |
---|
26 | int main() { |
---|
27 | constexpr static_poly<int, 3> x2p1; // zeros |
---|
28 | constexpr static_poly<int, 2> xm1; // zeros |
---|
29 | constexpr auto prod = x2p1 * xm1; |
---|
30 | return 0; |
---|
31 | } |
---|