Ticket #6818: tuple_subscript.h

File tuple_subscript.h, 1006 bytes (added by d.starosud@…, 11 years ago)
Line 
1#ifndef TUPLE_SUBSCRIPT_H
2#define TUPLE_SUBSCRIPT_H
3
4#include <tuple>
5
6template< std::size_t > struct Number2Type { };
7
8template< class... Ts >
9class tupless: public std::tuple<Ts...>
10{
11public:
12 template< class... ARGS >
13 tupless(ARGS... args): std::tuple<Ts...>(args...)
14 {
15 }
16 template< std::size_t N >
17 auto operator[](Number2Type<N>) const ->
18 decltype(std::get<N>(std::tuple<Ts...>())) const&
19 {
20 return std::get<N>(*this);
21 }
22 template< std::size_t N >
23 auto operator[](Number2Type<N>) ->
24 decltype(std::get<N>(std::tuple<Ts...>())) &
25 {
26 return std::get<N>(*this);
27 }
28};
29
30template< int N >
31constexpr std::size_t chars_to_int(const char (&array)[N], int current = 0, std::size_t acc = 0)
32{
33 return (current >= N || array[current] == 0) ?
34 acc
35 : chars_to_int(array, current + 1, 10 * acc + array[current] - '0');
36};
37
38template<char... Cs>
39constexpr auto operator "" _t() -> Number2Type<chars_to_int((const char[1 + sizeof...(Cs)]){Cs..., '\0'})>
40{
41 return {};
42};
43
44
45#endif //TUPLE_SUBSCRIPT_H