C++ Library Extensions 2022.12.09
To help learn modern C++ programming
24-first_n_types.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3namespace types = tpf::types;
4
7
8template<typename... Types>
10
11template<size_t FirstN, typename LeftList, typename RightList> struct first_n_types_list_st;
12
13template<>
15{
17};
18
19template<typename RightType, typename... RightTypes>
20struct first_n_types_list_st<0, type_list_t<>, type_list_t<RightType, RightTypes...>>
21{
23};
24
25template<typename... LeftTypes>
27{
28 using type = type_list_t<LeftTypes...>;
29};
30
31template<typename... LeftTypes, typename RightType, typename... RightTypes>
32struct first_n_types_list_st<1, type_list_t<LeftTypes...>, type_list_t<RightType, RightTypes...>>
33{
34 using type = type_list_t<LeftTypes..., RightType>;
35};
36
37template<size_t FirstN, typename... LeftTypes, typename RightType, typename... RightTypes>
38struct first_n_types_list_st<FirstN, type_list_t<LeftTypes...>, type_list_t<RightType, RightTypes...>>
39{
40 using type_0 = type_list_t<LeftTypes...>;
41
42 using type_1 = std::conditional_t<FirstN==1, type_list_t<LeftTypes..., RightType>,
43 typename first_n_types_list_st<FirstN-1, type_list_t<LeftTypes..., RightType>, type_list_t<RightTypes...>>::type>;
44
45 using type = std::conditional_t<FirstN==0, type_0, type_1>;
46};
47
48template<size_t FirstN, typename... Types>
50 typename first_n_types_list_st<FirstN, type_list_t<>, Types...>::type;
51
52template<size_t FirstN, typename... Types>
54{
55 static_assert(FirstN <= sizeof...(Types), "FirstN out of range");
56 using type =
58};
59
60template<size_t FirstN, typename... Types>
61struct first_n_types_st<FirstN, type_list_t<Types...>>
62{
63 static_assert(FirstN <= sizeof...(Types), "FirstN out of range");
64
65 using type =
67};
68
69template<size_t FirstN, typename... Types>
70using first_n_types_t = typename first_n_types_st<FirstN, Types...>::type;
71
73{
74 using types_t = type_list_t<short, int, double>;
75 // using types_t = type_list_t<>;
76
77 stream <<"Types: " << types_t{} << endl;
78
80 stream << "t_0_0: " << t_0_0{} << endl;
81
83 stream << "t_0_1: " << t_0_1{} << endl;
84
86 stream << "t_0_2: " << t_0_2{} << endl;
87
89 stream << "t_0_3: " << t_0_3{} << endl;
90
92 stream << "t_0_4: " << t_0_4{} << endl;
93}
94
96{
97 using types_t = type_list_t<short, int, double>;
98 // using types_t = type_list_t<>;
99
100 stream <<"Types: " << types_t{} << endl;
101
102 using t_0_0 = first_n_types_t<0, types_t>;
103 stream << "t_0_0: " << t_0_0{} << endl;
104
105 using t_0_1 = first_n_types_t<1, types_t>;
106 stream << "t_0_1: " << t_0_1{} << endl;
107
108 using t_0_2 = first_n_types_t<2, types_t>;
109 stream << "t_0_2: " << t_0_2{} << endl;
110
111 using t_0_3 = first_n_types_t<3, types_t>;
112 stream << "t_0_3: " << t_0_3{} << endl;
113
114 // using t_0_4 = first_n_types_t<4, types_t>;
115 // stream << "t_0_4: " << t_0_4{} << endl;
116
117 stream << endl;
118
120 stream << "t_1_0: " << t_1_0{} << endl;
121
123 stream << "t_1_1: " << t_1_1{} << endl;
124
126 stream << "t_1_2: " << t_1_2{} << endl;
127
129 stream << "t_1_3: " << t_1_3{} << endl;
130
131 // using t_1_4 = first_n_types_t<4, short, int, double>;
132 // stream << "t_1_4: " << t_1_4{} << endl;
133}
134
135int main()
136{
137 // test_first_n_types_list();
138
140}
141
142
143
tpf::sstream stream
typename first_n_types_list_st< FirstN, type_list_t<>, Types... >::type first_n_types_list_t
void test_first_n_types_t()
auto endl
void test_first_n_types_list()
int main()
typename first_n_types_st< FirstN, Types... >::type first_n_types_t
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endl
Definition: tpf_output.hpp:973
std::conditional_t< FirstN==1, type_list_t< LeftTypes..., RightType >, typename first_n_types_list_st< FirstN-1, type_list_t< LeftTypes..., RightType >, type_list_t< RightTypes... > >::type > type_1
typename first_n_types_list_st< FirstN, type_list_t<>, type_list_t< Types... > >::type type
typename first_n_types_list_st< FirstN, type_list_t<>, type_list_t< Types... > >::type type
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.