C++ Library Extensions 2022.12.09
To help learn modern C++ programming
33-tuple_variant.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3namespace types = tpf::types;
6
8{
9 using name_t = std::string;
10 using age_t = int;
11 using weight_t = double;
12
13 using name_age_vt_t = std::variant<name_t, age_t>;
14 using age_weight_vt_t = std::variant<age_t, weight_t>;
15
16 // tuple of variants
17 using tuple_t = std::tuple<name_age_vt_t, age_weight_vt_t>;
18
19 tuple_t tpl { name_age_vt_t{ "Sophie Turner" }, age_weight_vt_t{ 22.0 / 7.0 } };
20
21 stream << "tpl = " << tpl << endl;
22
23 auto& name_age = std::get<name_age_vt_t>(tpl);
24
25 name_age = 30;
26
27 stream << "tpl = " << tpl << endl;
28}
29
31{
32 using name_t = std::string;
33 using age_t = int;
34 using weight_t = double;
35
36 using name_age_tpl_t = std::tuple<name_t, age_t>;
37 using age_weight_tpl_t = std::tuple<age_t, weight_t>;
38
39 // a variant of tuples
40 using variant_t = std::variant<name_age_tpl_t, age_weight_tpl_t>;
41
42 variant_t vt { name_age_tpl_t{"Sophie", 20 } };
43
44 stream << "vt = " << vt << endl;
45
46 vt = age_weight_tpl_t{40, 45.6 };
47
48 stream << "vt = " << vt << endl;
49}
50
51int main()
52{
53 // test_tuple_of_variants();
54
56
57}
void test_variant_of_tuples()
tpf::sstream stream
void test_tuple_of_variants()
auto endl
int main()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.