Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <type_traits>
- #include <utility>
- namespace detail
- {
- template <std::size_t Index, typename ...Ts>
- struct get
- {
- static_assert(Index < sizeof...(Ts), "types_list::get index out of bounds");
- private:
- template <std::size_t CurrentIndex, typename ...Us>
- struct helper
- {
- using type = void;
- };
- template <std::size_t CurrentIndex, typename U, typename ...Us>
- struct helper<CurrentIndex, U, Us...>
- {
- using type = std::conditional_t<CurrentIndex == Index, U, typename helper<CurrentIndex + 1, Us...>::type>;
- };
- public:
- using type = typename helper<0, Ts...>::type;
- };
- template <template <typename...> typename TL, typename ...Ts>
- struct list_impl
- {
- using unpack = TL<Ts...>;
- inline static constexpr std::size_t size = sizeof...(Ts);
- template <std::size_t Index>
- using get = typename detail::get<Index, Ts...>::type;
- template <typename ...Us>
- using push_back = typename list_impl<TL, Ts..., Us...>::unpack;
- };
- }
- template <typename ...Ts>
- struct types_list : public detail::list_impl<types_list, Ts...>
- {
- };
- template <typename T, typename ...Ts>
- struct types_list<T, Ts...> : public detail::list_impl<types_list, T, Ts...>
- {
- private:
- using impl = detail::list_impl<types_list, T, Ts...>;
- public:
- using front = typename impl:: template get<0>;
- using back = typename impl:: template get<impl::size - 1>;
- };
- using t = types_list<int, double>::front;
- using t2 = types_list<int, double>::back;
- using t3 = types_list<int, char, double>::get<1>;
- using t4 = types_list<>::push_back<int>::push_back<std::pair<int,int>>::back;
- int main()
- {
- t x = 10;
- t2 y = 1.4;
- t3 z = 'a';
- t4 v = {1,1};
- }
Advertisement
Add Comment
Please, Sign In to add comment