-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
38 lines (35 loc) · 1.01 KB
/
Source.cpp
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
#include <array>
#include <iostream>
#include <utility>
template <typename T, size_t N, std::size_t... I>
constexpr std::array<T, N> compiletime_sort_impl(const std::array<T, N> &elems_in, std::index_sequence<I...>)
{
T elems[N] = { elems_in[I]...};
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N - 1 - i; j++)
{
if (elems[j] > elems[j+1])
{
T t = elems[j + 1];
elems[j + 1] = elems[j];
elems[j] = t;
}
}
}
return { elems[I]... };
}
template <typename T, size_t N, typename Indices = std::make_index_sequence<N>>
constexpr std::array<T, N> compiletime_sort(const std::array<T, N> &elems_in)
{
return compiletime_sort_impl(elems_in, Indices());
}
int main() {
constexpr std::array<int, 10> sample_arr2 = { 10,9,8,100500,6,5,4,3,2,1 };
constexpr auto rr = compiletime_sort(sample_arr2);
for (auto &elem : rr)
{
std::cout << elem << std::endl;
}
return 0;
}