-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc++14.cpp
More file actions
52 lines (43 loc) · 1.31 KB
/
c++14.cpp
File metadata and controls
52 lines (43 loc) · 1.31 KB
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
#include <tuple>
#include <iostream>
#include <array>
#include <utility>
#include <vector>
// Convert array into a tuple
template<typename Array, std::size_t... I>
decltype(auto) a2t_impl(const Array& a, std::index_sequence<I...>)
{
return std::make_tuple(a[I]...);
}
template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
decltype(auto) a2t(const std::array<T, N>& a)
{
return a2t_impl(a, Indices{});
}
// pretty-print a tuple
template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple_impl(std::basic_ostream<Ch,Tr>& os,
const Tuple & t,
std::index_sequence<Is...>)
{
((os << (Is == 0? "" : ", ") << std::get<Is>(t)), ...);
}
template<class Ch, class Tr, class... Args>
decltype(auto) operator<<(std::basic_ostream<Ch, Tr>& os,
const std::tuple<Args...>& t)
{
os << "(";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << ")";
}
int main()
{
std::array<int, 4> array {{1,2,3,4}};
std::vector<int> ivec;
// convert an array into a tuple
auto tuple = a2t(array);
static_assert(std::is_same<decltype(tuple),
std::tuple<int, int, int, int>>::value, "");
// print it to cout
std::cout << tuple << '\n';
}