-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_init.cc
69 lines (57 loc) · 1.3 KB
/
array_init.cc
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
#include <array>
#include <iostream>
namespace {
constexpr int N = 6;
constexpr int f(int x, int y) {
return x * y;
}
template <typename T, T... t, typename... U>
constexpr auto fs(U... u) {
return std::array<T, sizeof... (t)>{{f(t, (0 + ... + u)) ...}};
}
template <int...>
struct S {
static constexpr auto gs() {
return fs<int>();
}
};
template <int... i>
struct S<0, i...> {
static constexpr auto gs() {
return fs<int, 0, i...>(2);
}
};
template <int i, int... j>
struct S<i, j...> {
static constexpr auto gs() {
return S<i - 1, i, j...>::gs();
}
};
constexpr auto X0 = fs<int>();
constexpr auto X1 = fs<int, 3, 5, 7>(2, 4);
constexpr auto X2 = S<>::gs();
constexpr auto X3 = S<N - 1>::gs();
constexpr auto X4 = S<N, 17, 20>::gs();
}
int main() {
std::cout << "X0:";
for (auto x : X0)
std::cout << ' ' << x;
std::cout << std::endl;
std::cout << "X1:";
for (auto x : X1)
std::cout << ' ' << x;
std::cout << std::endl;
std::cout << "X2:";
for (auto x : X2)
std::cout << ' ' << x;
std::cout << std::endl;
std::cout << "X3:";
for (auto x : X3)
std::cout << ' ' << x;
std::cout << std::endl;
std::cout << "X4:";
for (auto x : X4)
std::cout << ' ' << x;
std::cout << std::endl;
}