C++ Library Extensions 2022.12.09
To help learn modern C++ programming
22-is_template_v.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6struct no_type_t { };
7
8template<typename Type>
9struct is_template_st // we assume Type is not a class template
10{
11 using type = no_type_t;
12 static constexpr bool value = false;
13};
14
15// typename, typename... means 1 or more
16// template<template<typename, typename...> class ClassTemplate,
17// typename Type, typename... Types>
18// struct is_template_st<ClassTemplate<Type, Types...>>
19// {
20// using type = Type;
21// static constexpr bool value = true;
22// };
23
24// typename... means zero or more
25template<template<typename...> class ClassTemplate, typename... Types>
26struct is_template_st<ClassTemplate<Types...>>
27{
28 using type = no_type_t;
29 static constexpr bool value = true;
30};
31
32template<typename Type>
33constexpr bool is_valid_type_v = !std::is_same_v<Type, no_type_t>;
34
35template<typename Type>
37
38template<typename Type>
40
42{
43 int a = 5;
44 std::vector<int> v{1, 2, 3, 4, 5};
45
46 stream << "The type of a: " << Tpf_GetTypeCategory(a) << endl;
47 stream << "Is \'a\' a template? " << is_template_v<decltype(a)> << endl;
48
49 stream << "The type of v: " << Tpf_GetTypeCategory(v) << endl;
50 stream << "Is \'v\' a template? " << is_template_v<decltype(v)> << endl;
51}
52
54{
55 int a = 5;
56 std::vector<double> v{4.6, 6.7};
57
58 if constexpr(is_template_v<decltype(a)>)
59 {
60 stream <<"\'a\' is a template" << endl;
61 }
62 else
63 {
64 stream <<"\'a\' is NOT a template" << endl;
65 }
66
67 if constexpr(is_template_v<decltype(v)>)
68 {
69 stream <<"\'v\' is a template" << endl;
70
71 using first_t = first_template_parameter_t<decltype(v)>;
72
73 stream <<"The type of the first parameter: "
74 << Tpf_GetTypeName(first_t) << endl;
75
76 }
77 else
78 {
79 stream <<"\'v\' is NOT a template" << endl;
80 }
81
82 using tuple_t = std::tuple<>;
83
84 if constexpr(is_template_v<tuple_t>)
85 {
86 stream <<"tuple_t is a template" << endl;
87
89
90 if constexpr(is_valid_type_v<first_t>)
91 {
92 stream <<"The first parameter type of tuple_t: "
93 << Tpf_GetTypeName(first_t) << endl;
94 }
95 else
96 {
97 stream <<"The first parameter type of tuple_t is EMPTY!!!"<< endl;
98 }
99 }
100 else
101 {
102 stream <<"tuple_t is NOt a template" << endl;
103 }
104
105
106}
107
108int main()
109{
110 // test_is_template_v();
111
113}
constexpr bool is_valid_type_v
tpf::sstream stream
void test_is_template_v()
constexpr bool is_template_v
void test_first_paremter_t()
auto endl
typename is_template_st< Type >::type first_template_parameter_t
int main()
constexpr auto endl
Definition: tpf_output.hpp:973
static constexpr bool value
Stream output operators << are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.
Definition: tpf_types.hpp:1422
#define Tpf_GetTypeCategory(instance_arg)
A macro that returns instance_arg's type category string name.
Definition: tpf_types.hpp:1428