Guest User

Untitled

a guest
Nov 24th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <cstdint>
  2. #include <type_traits>
  3. #include <utility>
  4.  
  5. namespace detail
  6. {
  7.     template <std::size_t Index, typename ...Ts>
  8.     struct get
  9.     {
  10.         static_assert(Index < sizeof...(Ts), "types_list::get index out of bounds");
  11.  
  12.     private:
  13.         template <std::size_t CurrentIndex, typename ...Us>
  14.         struct helper
  15.         {
  16.             using type = void;
  17.         };
  18.  
  19.         template <std::size_t CurrentIndex, typename U, typename ...Us>
  20.         struct helper<CurrentIndex, U, Us...>
  21.         {
  22.             using type = std::conditional_t<CurrentIndex == Index, U, typename helper<CurrentIndex + 1, Us...>::type>;
  23.         };
  24.  
  25.     public:
  26.         using type = typename helper<0, Ts...>::type;
  27.     };
  28.  
  29.     template <template <typename...> typename TL, typename ...Ts>
  30.     struct list_impl
  31.     {
  32.         using unpack = TL<Ts...>;
  33.  
  34.         inline static constexpr std::size_t size = sizeof...(Ts);
  35.  
  36.         template <std::size_t Index>
  37.         using get = typename detail::get<Index, Ts...>::type;
  38.  
  39.         template <typename ...Us>
  40.         using push_back = typename list_impl<TL, Ts..., Us...>::unpack;
  41.     };
  42. }
  43.  
  44. template <typename ...Ts>
  45. struct types_list : public detail::list_impl<types_list, Ts...>
  46. {
  47. };
  48.  
  49. template <typename T, typename ...Ts>
  50. struct types_list<T, Ts...> : public detail::list_impl<types_list, T, Ts...>
  51. {
  52. private:
  53.     using impl = detail::list_impl<types_list, T, Ts...>;
  54.  
  55. public:
  56.     using front = typename impl:: template get<0>;
  57.     using back = typename impl:: template get<impl::size - 1>;
  58. };
  59.  
  60. using t = types_list<int, double>::front;
  61. using t2 = types_list<int, double>::back;
  62. using t3 = types_list<int, char, double>::get<1>;
  63. using t4 = types_list<>::push_back<int>::push_back<std::pair<int,int>>::back;
  64.  
  65. int main()
  66. {
  67.     t x = 10;
  68.     t2 y = 1.4;
  69.     t3 z = 'a';
  70.     t4 v = {1,1};
  71. }
Advertisement
Add Comment
Please, Sign In to add comment