C++ Library Extensions 2022.12.09
To help learn modern C++ programming
array_tuple_conversion.cpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Feb. 03, 2021
4
5 Requirements: C++ compilers that supports C++17 Standards
6
7 clang++ -std=c++17 filename.cpp -ltbb -o output.exe
8 g++ -std=c++17 filename.cpp -ltbb -o output.exe
9 cl /EHsc /std:c++17 filename.cpp /Fe: output.exe
10 dpcpp -Qstd=c++17 filename.cpp -o output.exe
11*/
12
13#include <tpf_output.hpp>
15
16namespace types = tpf::types;
17
19auto& endl = tpf::endl; // one carriage-return and flush out to console
20auto& endL = tpf::endL; // two carriage-returns and flush out to console
21
23{
24 stream << "\n--- File Contents Summary ---\n\n1. Conversion from std::tuple to other container classes" << endL;
25
26 stream << "2. Conversion from std::array to other container classes" << endL;
27
28 stream <<"For details, read the source code below." << endL;
29
30}
31
33{
34 stream <<"--- Conversion from std::tuple to other container classes" << endL;
35
36 std::tuple t1 { 1, 3.5, 4.6f, 2.3f, 3u };
37
38 auto a1 = types::convert_to_array(t1);
39
40 stream <<"From " << Tpf_GetTypeCategory(t1) <<" to " << Tpf_GetTypeCategory(a1) << endl;
41 stream <<"std::tuple "<<t1<<" is converted to std::array " << a1 << endL;
42
43 auto t2 = types::convert_to_tuple(a1);
44 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to " << Tpf_GetTypeCategory(t2) << endl;
45 stream <<"std::array "<< a1 <<" is converted to std::tuple " << t2 << endL;
46
47 auto v1 = types::convert_to_container<std::vector>(t1);
48 stream <<"From " << Tpf_GetTypeCategory(t1) <<" to " << Tpf_GetTypeCategory(v1) << endl;
49 stream <<"std::tuple "<< t1 <<" is converted to std::vector " << v1 << endL;
50
51 auto d1 = types::convert_to_container<std::deque>(t1);
52 stream <<"From " << Tpf_GetTypeCategory(t1) <<" to " << Tpf_GetTypeCategory(d1) << endl;
53 stream <<"std::tuple "<< t1 <<" is converted to std::deque " << d1 << endL;
54
55 // STEP 1. Convert the type of t1, tuple, to vector of variants
57 types::tuple_to_container_of_variants_t<std::vector, decltype(t1)>;
58
59 // STEP 2. Convert tuple t1 to vector of variants vv
60 auto vv = types::convert_to_container<vector_of_variants_t>(t1);
61
62 stream <<"From " << Tpf_GetTypeCategory(t1) <<" to\n " << Tpf_GetTypeCategory(vv) << endl;
63 stream <<"std::tuple "<< t1 <<" is converted to vector of variants " << vv << endL;
64
65 // STEP 1. Convert the type of t1, tuple, to deque of variants
66 using deque_of_variants_t =
67 types::tuple_to_container_of_variants_t<std::deque, decltype(t1)>;
68
69 // STEP 2. Convert tuple t1 to deque of variants dv
70 auto dv = types::convert_to_container<deque_of_variants_t>(t1);
71
72 stream <<"From " << Tpf_GetTypeCategory(t1) <<" to\n " << Tpf_GetTypeCategory(dv) << endl;
73 stream <<"std::tuple "<< t1 <<" is converted to deque of variants " << dv << endL;
74
75}
76
78{
79 stream <<"--- Conversion from std::array to other container classes" << endL;
80
81 std::array a1 { 1.3, 3.5, 4.6, 2.3, 3.0 };
82
83 auto t1 = types::convert_to_tuple(a1);
84
85 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to " << Tpf_GetTypeCategory(t1) << endl;
86 stream <<"std::array "<< a1 <<" is converted to std::tuple " << t1 << endL;
87
88 auto v1 = types::convert_to_container<std::vector>(a1);
89 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to " << Tpf_GetTypeCategory(v1) << endl;
90 stream <<"std::array "<< a1 <<" is converted to std::vector " << v1 << endL;
91
92 auto d1 = types::convert_to_container<std::deque>(t1);
93 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to " << Tpf_GetTypeCategory(d1) << endl;
94 stream <<"std::array "<< a1 <<" is converted to std::deque " << d1 << endL;
95
96 auto ms = types::convert_to_container<std::multiset>(a1);
97 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to " << Tpf_GetTypeCategory(ms) << endl;
98 stream <<"std::array "<< a1 <<" is converted to multiset " << ms << endL;
99
100 auto ums = types::convert_to_container<std::unordered_multiset>(a1);
101 stream <<"From " << Tpf_GetTypeCategory(a1) <<" to\n " << Tpf_GetTypeCategory(ums) << endl;
102 stream <<"std::array "<< a1 <<" is converted to unordered_multiset " << ums << endL;
103
104 stream <<"\t\t--- By Thomas Kim, Feb. 05, 2020. ---" << endl;
105}
106
107int main()
108{
110
112
114}
void test_conversion_from_tuple_to_container()
void summary_array_tuple_converstion()
auto & endl
tpf::sstream stream
auto & endL
void test_conversion_from_array_to_container()
container_of_variants_t< std::vector, ElementTypes... > vector_of_variants_t
Definition: tpf_types.hpp:6558
container_of_variants_t< std::deque, ElementTypes... > deque_of_variants_t
Definition: tpf_types.hpp:6561
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
auto convert_to_array(const std::tuple< Type, Types... > &tuple)
Definition: tpf_types.hpp:5835
Container< types::to_variant_t< remove_cvref_t< TupleType > > > tuple_to_container_of_variants_t
Definition: tpf_types.hpp:5636
auto convert_to_tuple(const std::array< T, Size > &array)
Definition: tpf_types.hpp:5883
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
Type functions are implemented.
#define Tpf_GetTypeCategory(instance_arg)
A macro that returns instance_arg's type category string name.
Definition: tpf_types.hpp:1428