C++ Library Extensions 2022.12.09
To help learn modern C++ programming
038-make_variants.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6template<typename Type>
7using remove_cv_ref_t = std::remove_cv_t<std::remove_reference_t<Type>>;
8
9template<typename Type>
11
12template<typename Type>
14{
15 using type = std::variant<Type>;
16};
17
18template<typename Type>
20{
21 using type = std::variant<Type>;
22};
23
24template<typename Type, typename... Types>
25struct to_variant_st<tpf::types::type_list_t<Type, Types...>>
26{
27 using type = std::variant<Type, Types...>;
28};
29
30template<typename Type, typename... Types>
31using to_variant_t = typename to_variant_st< tpf::types::unique_types_t<Type, Types...> >::type;
32
33template<typename Type, typename... Types>
34auto make_variants(Type&& arg, Types&&... args)
35{
36 using parameter_types =
38
39 if constexpr(tpf::types::common_type_v<parameter_types>)
40 {
41 // common type exists
43
44 return std::vector<common_type>{
45 static_cast<common_type>(std::forward<Type>(arg)),
46 static_cast<common_type>(std::forward<Types>(args))... };
47
48 }
49 else
50 {
51 // common type does not exists
52
54
55 return std::vector<element_t>{ element_t{std::forward<Type>(arg)},
56 element_t{ std::forward<Types>(args) }... };
57 }
58
59}
60
62{
63 const int a = 5;
64 // auto v = make_variants(a);
65
66 auto v2 = make_variants("String Text", 4, 4.5f, 5.6);
67}
68
70{
71 // common type is double
72 auto v1 = make_variants(1, 2.0f, 3.14, 3.6, 5u, 6ull);
73
74 // stream << "Type of v1: " << Tpf_GetTypeCategory(v1) << endl;
75 stream << "v1: " << v1 << endl << endl;
76
77 // common type is double
78 auto v2 = make_variants("New String", 1, 2.0f, 3.14, "String Literal",
79 std::string("I love it"), L"Wide Character String");
80
81 // stream << "Type of v2: " << Tpf_GetTypeCategory(v2) << endl;
82 stream << "v2: " << v2 << endl;
83}
84
85int main()
86{
87 // test_make_variants();
88
90}
std::remove_cv_t< std::remove_reference_t< std::decay_t< Type > > > decay_remove_cv_ref_t
void test_make_variants()
tpf::sstream stream
void test_make_variants_two()
auto endl
std::remove_cv_t< std::remove_reference_t< Type > > remove_cv_ref_t
typename to_variant_st< tpf::types::unique_types_t< Type, Types... > >::type to_variant_t
int main()
auto make_variants(Type &&arg, Types &&... args)
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
Includes subnamespace conversion.
Definition: 31-visit.cpp:7
constexpr auto endl
Definition: tpf_output.hpp:973
std::variant< Type > type
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.