C++ Library Extensions 2022.12.09
To help learn modern C++ programming
07-type_list.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6namespace types = tpf::types;
7
8template<typename... Types>
9void test_what_is_type_list(Types... args)
10{
11 using types_t = types::type_list_t<Types...>;
12
13 stream <<"Types of arguments: " << types_t{} << endl;
14
15 stream << "The count types: " << types::type_count_v<types_t> << endl;
16
21
22 stream << "First type: " << Tpf_GetTypeName(first_t) << endl;
23 stream << "Second type: " << Tpf_GetTypeName(second_t) << endl;
24 stream << "Third type: " << Tpf_GetTypeName(third_t) << endl;
25 stream << "Last type: " << Tpf_GetTypeName(last_t) << endl;
26
28
29 stream << "Common type: " << Tpf_GetTypeName(common_t) << endl;
30 }
31
32template<typename... Types>
33void how_to_use_type_list(Types... args)
34{
35 using types_t = types::type_list_t<Types...>;
36
37 if constexpr(types::common_type_v<types_t>)
38 {
40
41 stream << "Common type exists : " << Tpf_GetTypeName(common_t) << endl;
42 }
43 else
44 {
45 stream << "The types are : " << types_t{} << endl;
46
47 stream << "Common type does not exist" << endl;
48 }
49
50 stream << "Is int in the list: " << types::is_in_list_v<int, types_t> << endl;
51
52 stream << "All types are the same: " << types::is_same_v<types_t> << endl;
53
54}
55
57{
59
60 stream <<"Original types: " << types_t{} << endl;
61
63
64 stream <<"Unique types: " << unique_types_t{} << endl;
65
67
68 stream << "The type of variant_t: " << Tpf_GetTypeName(variant_t) << endl;
69
70}
71
72// template<template<typename, typename...> class ContainerType, typename... ElementTypes>
73// class container_of_variant_t: public ContainerType < types::to_variant_t<
74// types::unique_types_t<ElementTypes...> > >
75// {
76// public:
77// using base_type = ContainerType < types::to_variant_t<
78// types::unique_types_t<ElementTypes...> > >;
79// using base_type::base_type;
80// };
81
82// template<typename... ElementTypes>
83// using vector_of_variants_t = container_of_variant_t<std::vector, ElementTypes...>;
84
85// template<typename... ElementTypes>
86// using deque_of_variants_t = container_of_variant_t<std::deque, ElementTypes...>;
87
88// template<typename... ElementTypes>
89// using list_of_variants_t = container_of_variant_t<std::list, ElementTypes...>;
90
91// template<typename... ElementTypes>
92// using set_of_variants_t = container_of_variant_t<std::set, ElementTypes...>;
93
95{
97
98 vv.emplace_back(1);
99 vv.emplace_back(22.0 /7.0);
100 vv.emplace_back("Thomas Kim");
101 vv.emplace_back("Sophie Turner");
102
103 stream <<"vv (vector of variants) = " << vv << endl;
104
106
107 lv.emplace_back(1);
108 lv.emplace_back(22.0 /7.0);
109 lv.emplace_back(L"Thomas Kim");
110 lv.emplace_back("Sophie Turner");
111
112 stream << "lv (list of variants) = "<< lv << endl;
113
114 // in case of container std::set,
115 // operator < () should be supported
117
118 sv.insert(1);
119 sv.insert(22.0 /7.0);
120 sv.insert(6ll);
121 sv.insert(7ul);
122
123 stream << "sv (set of variants) = " << sv << endl;
124}
125
126
127int main()
128{
129 // test_what_is_type_list(1, 2u, 3.0f, 4.0);
130
131 // how_to_use_type_list(1, 2u, 3.0f, 4.0);
132
133 // how_to_use_type_list(1, 2u, 3.0f, 4.0, "Thomas Kim");
134
135 // how_to_use_type_list(1.0, 2.0, 4.0);
136
137 // test_advanced_type_list();
138
140
141}
tpf::sstream stream
Definition: 07-type_list.cpp:3
void test_advanced_type_list()
void how_to_use_type_list(Types... args)
void test_what_is_type_list(Types... args)
Definition: 07-type_list.cpp:9
auto endl
Definition: 07-type_list.cpp:4
int main()
void test_containers_of_variants()
std::common_type_t< S, T > common_t
Definition: cpg_bitwise.hpp:79
std::ratio< 1 > second_t
typename unique_types_st< type_list_t<>, Types... >::type unique_types_t
Definition: tpf_types.hpp:4598
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
hidden::select_first_type_t< Types... > select_first_type_t
Definition: tpf_types.hpp:5567
hidden::vector_of_variants_t< ElementTypes... > vector_of_variants_t
Definition: tpf_types.hpp:6621
hidden::unique_types_t< Types... > unique_types_t
Definition: tpf_types.hpp:5627
hidden::common_type_t< Types... > common_type_t
Definition: tpf_types.hpp:5122
hidden::select_last_type_t< Types... > select_last_type_t
Definition: tpf_types.hpp:5576
hidden::to_variant_t< Type, Types... > to_variant_t
Definition: tpf_types.hpp:5630
hidden::set_of_variants_t< ElementTypes... > set_of_variants_t
Definition: tpf_types.hpp:6630
hidden::select_nth_type_t< SelectIndex, Types... > select_nth_type_t
Definition: tpf_types.hpp:5585
hidden::list_of_variants_t< ElementTypes... > list_of_variants_t
Definition: tpf_types.hpp:6627
constexpr auto endl
Definition: tpf_output.hpp:973
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.
Definition: tpf_types.hpp:1422