1 // The Art of C++ / Sequences
2 // Copyright (c) 2015 Daniel Frey
3 
4 #ifndef TAOCPP_SEQUENCES_INCLUDE_TYPE_BY_INDEX_HPP
5 #define TAOCPP_SEQUENCES_INCLUDE_TYPE_BY_INDEX_HPP
6 
7 #include <cstddef>
8 #include <type_traits>
9 
10 #include "make_integer_sequence.hpp"
11 
12 namespace tao
13 {
14   namespace seq
15   {
16     // based on http://stackoverflow.com/questions/18942322
17 
18     namespace impl
19     {
20       template< std::size_t >
21       struct any
22       {
23         any( ... );
24       };
25 
26       template< typename >
27       struct wrapper;
28 
29       template< typename >
30       struct unwrap;
31 
32       template< typename T >
33       struct unwrap< wrapper< T > >
34       {
35         using type = T;
36       };
37 
38       template< typename >
39       struct get_nth;
40 
41       template< std::size_t... Is >
42       struct get_nth< index_sequence< Is... > >
43       {
44         template< typename T >
45         static T deduce( any< Is & 0 >..., T*, ... );
46       };
47     }
48 
49     template< std::size_t I, typename... Ts >
50     using type_by_index = impl::unwrap< decltype( impl::get_nth< make_index_sequence< I > >::deduce( std::declval< impl::wrapper< Ts >* >()... ) ) >;
51 
52     template< std::size_t I, typename... Ts >
53     using type_by_index_t = typename type_by_index< I, Ts... >::type;
54   }
55 }
56 
57 #endif // TAOCPP_SEQUENCES_INCLUDE_TYPE_BY_INDEX_HPP
58