C++ Library Extensions 2022.12.09
To help learn modern C++ programming
13-type_list.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6namespace types = tpf::types;
7
9{
10 using integer_types_t =
12
13 using real_types_t =
15
16
17 stream <<"is int in integer_types_t ? "
18 << types::is_in_list_v<int, integer_types_t> << endl;
19
20 stream <<"is int in real_types_t ? "
21 << types::is_in_list_v<int, real_types_t> << endl;
22
23
24 stream <<"is float in integer_types_t ? "
25 << types::is_in_list_v<float, integer_types_t> << endl;
26
27 stream <<"is float in real_types_t ? "
28 << types::is_in_list_v<float, real_types_t> << endl;
29
30}
31
32template<typename Type, typename... Types>
34
35template<typename Type> // for integral type
37remainder(Type n, Type d)
38{
39 return n % d;
40}
41
42template<typename Type> // for real type
44remainder(Type n, Type d)
45{
46 long long q = (long long) n/d;
47
48 return (n - q*d);
49}
50
52{
53 stream << "The remainder of 7.0 divided by 4.0 is "
54 << remainder(7.0, 4.0) << endl;
55
56 stream << "The remainder of 7 divided by 4 is "
57 << remainder(7, 4) << endl;
58}
59
60
61template<typename Type>
62Type remainder_fn(Type n, Type d)
63{
64 using integral_types = types::type_list_t<short, unsigned short,
65 int, unsigned, long, unsigned long, long long, unsigned long long>;
66
67 using real_types = types::type_list_t<float, double>;
68
69 // if Type is of integral types
70 if constexpr(types::is_in_list_v<Type, integral_types>)
71 {
72 return n % d;
73 }
74 else if constexpr(types::is_in_list_v<Type, real_types>)
75 {
76 long long q = (long long) n / d;
77 return (n - q * d);
78 }
79 else
80 {
81 static_assert(types::is_in_list_v<Type, real_types>
82 || types::is_in_list_v<Type, integral_types>, "Type should be either integral of floating-point type");
83 }
84
85}
86
88{
89 stream << "The remainder of 7.0 divided by 4.0 is "
90 << remainder_fn(7.0, 4.0) << endl;
91
92 stream << "The remainder of 7 divided by 4 is "
93 << remainder_fn(7, 4) << endl;
94
95}
96int main()
97{
98 // test_what_is_type_list();
99
100 // test_remainder();
101
103}
typename enable_if< predicate, ReturnType >::type enable_if_t
Definition: 12_sfinae.cpp:23
tpf::sstream stream
Definition: 13-type_list.cpp:3
void test_what_is_type_list()
Definition: 13-type_list.cpp:8
std::enable_if_t< types::is_in_list_v< Type, Types... >, Type > enable_if_in_types_t
void test_remainder()
Type remainder_fn(Type n, Type d)
auto endl
Definition: 13-type_list.cpp:4
void test_remainder_fn()
enable_if_in_types_t< Type, short, unsigned short, int, unsigned, long, unsigned long, long long, unsigned long long > remainder(Type n, Type d)
int main()
constexpr bool is_in_list_v
Definition: tpf_types.hpp:3841
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endl
Definition: tpf_output.hpp:973
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.