C++ Library Extensions 2022.12.09
To help learn modern C++ programming
21-is_template.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3namespace types = tpf::types;
4
7
8template<typename Type>
9void is_template_class(Type value)
10{
11 stream <<"The type of Type: " << Tpf_GetTypeName(Type) << endl;
12 stream <<"Is type a template? " << types::is_template_v<Type> << endl;
13 stream << "The value : " << value << endl << endl;
14}
15
16
18{
19 int i = 50;
21
22 std::vector<int> v{1, 2, 3, 4, 5};
23
25}
26
27template<typename Type1, typename Type2>
28void extract_template_arguments(Type1 arg1, Type2 arg2)
29{
30 if constexpr(types::is_template_v<Type1>)
31 {
32 stream << "Type1 is a template class " << endl;
33 stream << "Type of Type1: " << Tpf_GetTypeName(Type1) << endl;
34 stream << "The first template argment of Type1 is "
36
37 stream << "All template arguments: "
39 }
40 else
41 {
42 stream << "Type1 " << Tpf_GetTypeName(Type1)
43 << " is not a template class." << endl;
44 }
45
46 stream << endl;
47
48 if constexpr(types::is_template_v<Type2>)
49 {
50 stream << "Type2 is a template class " << endl;
51 stream << "Type of Type2: " << Tpf_GetTypeName(Type2) << endl;
52 stream << "The first template argment of Type2 is "
54
55 stream << "All template arguments: "
57 }
58 else
59 {
60 stream << "Type2 " << Tpf_GetTypeName(Type2)
61 << " is not a template class." << endl;
62 }
63
64 stream << endl;
65
66}
67
69{
70 std::vector<int> v { 1, 2, 3, 4, 5};
71 std::map<std::string, double> m;
72
73 m["apple price"] = 100.0;
74 m["peach price"] = 300.0;
75
77}
78
80{
81 using tuple_t = std::tuple<int, double, short, const char*>;
82
83 // extract template arguments from tuple_t
84 using template_arguments = types::template_parameter_type_list_t<tuple_t>;
85
88
89 stream <<"The type of tuple_t: " << Tpf_GetTypeName(tuple_t) << endl;
90 stream << "The first argument: " << Tpf_GetTypeName(first_t) << endl;
91 stream << "The last argument: " << Tpf_GetTypeName(last_t) << endl;
92 stream << "All template arguments: " << template_arguments{} << endl;
93
94 stream << endl;
95
96 using variant_t = std::variant<int, double, short, const char*, long long>;
97
98 // extract template arguments from tuple_t
99 using template_arguments1 = types::template_parameter_type_list_t<variant_t>;
100
103
104 stream <<"The type of variant_t: " << Tpf_GetTypeName(variant_t) << endl;
105 stream << "The first argument: " << Tpf_GetTypeName(first_t1) << endl;
106 stream << "The last argument: " << Tpf_GetTypeName(last_t1) << endl;
107 stream << "All template arguments: " << template_arguments1{} << endl;
108
109}
110
111int main()
112{
113 // test_if_template();
114
115 // test_extract_template_arguments();
116
118}
tpf::sstream stream
void test_if_template()
void test_template_argument_extraction()
void extract_template_arguments(Type1 arg1, Type2 arg2)
auto endl
void is_template_class(Type value)
void test_extract_template_arguments()
int main()
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::template_parameter_type_list_t< Type > template_parameter_type_list_t
Definition: tpf_types.hpp:3393
hidden::select_last_type_t< Types... > select_last_type_t
Definition: tpf_types.hpp:5576
hidden::first_parameter_type_t< Type > first_parameter_type_t
Definition: tpf_types.hpp:5591
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.
Definition: tpf_types.hpp:1422