-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetindex.h
43 lines (33 loc) · 1.02 KB
/
getindex.h
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
#ifndef _GETINDEX_H
#define _GETINDEX_H
// provides pack_tools::get_index<I>(Ts&&... ts)
// (similar to what std::get<I>(std::make_tuple(ts...)) does)
namespace pack_tools {
namespace detail {
template <unsigned int> struct int_c {};
template <unsigned int I>
constexpr void *get_index_impl(int_c<I>) // invalid index
{
return {};
}
template <typename T0,typename... Ts>
constexpr T0&& get_index_impl(int_c<0>,T0&& t0,Ts&&... ts)
{
return (T0&&)t0;
}
template <unsigned int I,typename T0,typename... Ts>
constexpr auto get_index_impl(int_c<I>,T0&& t0,Ts&&... ts)
-> decltype(get_index_impl(int_c<I-1>(),(Ts&&)ts...))
{
return get_index_impl(int_c<I-1>(),(Ts&&)ts...);
}
} // namespace detail
template <unsigned int I,typename... Ts>
constexpr auto get_index(Ts&&... ts)
-> decltype(detail::get_index_impl(detail::int_c<I>(),(Ts&&)ts...))
{
static_assert((I<sizeof...(Ts)),"Invalid Index");
return detail::get_index_impl(detail::int_c<I>(),(Ts&&)ts...);
}
} // namespace pack_tools
#endif