C++ Library Extensions 2022.12.09
To help learn modern C++ programming
11-type_dispatch.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2/*
3 The primary reason for type dispatch is to resolve
4 conflicting function overloading.
5
6 For example,
7
8 template<typename RealType>
9 RealType remainer(RealType n, RealType d, true_type)
10 {
11
12 }
13
14 template<typename IntType>
15 IntType remainer(IntType n, IntType d, false_type)
16 {
17
18 }
19
20 In C++14 stanard, _v suffix was introduced as below
21
22 */
25
26double remainder(double n, double d, double )
27{
28 long long q = (long long)n/d;
29
30 return (n - q * d);
31}
32
34{
35 auto result_d = remainder(7.0, 4.0, 0.0);
36
37 stream << "result_d = " << result_d << endl;
38}
39
40template<typename Type, Type v>
42{
43 static constexpr Type value = v;
44 using value_type = Type;
45
47
48 // conversion to value_type (or Type) operator
49 constexpr operator value_type() const noexcept
50 {
51 return value;
52 }
53
54 // function call operator()
55 constexpr Type operator()() const noexcept
56 {
57 return value;
58 }
59};
60
63
64template<typename Type_1, typename Type_2>
65struct is_same // we assume Type_1 and Type_2 are different
66{
67 // we defined an instance value of type false_type
68 static constexpr false_type value = false_type{};
70};
71
72template<typename Type>
73struct is_same<Type, Type> // Type, Type are the same
74{
75 // we defined an instance value of type true_type
76 static constexpr true_type value = true_type{};
77 using type = true_type;
78};
79
80// defined since C++14 standard
81template<typename Type_1, typename Type_2>
83
84constexpr bool is_same_type(true_type /* parameter name is missing */)
85{
86 return true;
87}
88
89constexpr bool is_same_type(false_type /* parameter name is missing */)
90{
91 return false;
92}
93
95{
96 stream <<"int, int : "
98
99 stream <<"int, double : "
101}
102
103template<typename RealType>
104RealType remainder_impl(RealType n, RealType d, true_type)
105{
106 long long q = (long long)n/d;
107
108 return (n - q * d);
109}
110
111template<typename IntType>
112IntType remainder_impl(IntType n, IntType d, false_type)
113{
114 return n % d;
115}
116
117template<typename Type>
118Type remainder(Type n, Type d)
119{
121}
122
124{
125 stream << "The remainder 7.0 divided by 4.0 is "
126 << remainder(7.0, 4.0) << endl;
127
128 stream << "The remainder 7 divided by 4 is "
129 << remainder(7, 4) << endl;
130}
131
133{
134 stream << "is_same_v<int, int>: " << is_same_v<int, int> << endl;
135
136 stream << "is_same_v<int, double>: " << is_same_v<int, double> << endl;
137}
138
139int main()
140{
141 // test_remainder();
142
143 // test_is_same();
144
145 // test_type_dispatch();
146
148}
constexpr bool is_same_type(true_type)
tpf::sstream stream
RealType remainder_impl(RealType n, RealType d, true_type)
void test_type_dispatch()
void test_is_same()
integral_constant< bool, true > true_type
void test_remainder()
integral_constant< bool, false > false_type
auto endl
double remainder(double n, double d, double)
constexpr auto is_same_v
int main()
void test_is_same_v()
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr Type operator()() const noexcept
static constexpr Type value
static constexpr false_type value
Stream output operators << are implemented.