-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConceptsRequiresFunctions.cpp
More file actions
243 lines (191 loc) · 6.2 KB
/
ConceptsRequiresFunctions.cpp
File metadata and controls
243 lines (191 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// =====================================================================================
// ConceptsRequiresFunctions.cpp
// =====================================================================================
module modern_cpp:concepts_requires_functions;
// using <concepts>
template <typename T>
concept Numerical = std::integral<T> or std::floating_point<T>;
// using <type_traits>
template <typename T>
concept NumericalEx = std::is_integral<T>::value or std::is_floating_point<T>::value;
namespace Requires_Clause {
template <typename T>
requires Numerical<T>
auto add(T a, T b)
{
return a + b;
}
// "inlining" constraints on template parameter types
template <typename T>
requires std::integral<T> or std::floating_point<T>
auto addEx(T a, T b)
{
return a + b;
}
static void test_concepts_requires_01()
{
auto sum1{ add(123.456f, 654.321f) };
std::cout << sum1 << std::endl;
auto sum2{ add(123.456, 654.321) };
std::cout << sum2 << std::endl;
// 'add': no matching overloaded function found
// template parameter 'T' is ambiguous
// could be 'float'
// or 'double'
// auto sum3 = add(123.456, 654.321F);
//'add': no matching overloaded function found
// the associated constraints are not satisfied
// the concept 'Numerical<std::string>' evaluated to false
// the concept 'std::floating_point<std::string>' evaluated to false
// the concept 'std::integral<std::string>' evaluated to false
// auto sum4 = add(std::string { "ABC" }, std::string { "DEF" });
}
// ---------------------------------------------------------------------------------
// several, different template parameters
template <typename T, typename U>
requires Numerical<T> and Numerical<U>
auto add(T a, U b)
{
return a + b;
}
static void test_concepts_requires_02()
{
auto sum{ add(123.456, 654.321F) };
std::cout << sum << std::endl;
}
static void test_concepts_requires_03()
{
std::string s1{ "ABC" };
std::string s2{ "DEF" };
// the associated constraints are not satisfied:
// auto result{ add(s1, s2) };
}
// using <type_traits>
template<typename T>
requires std::is_integral<T>::value
double avg(const std::vector<T>& vec)
{
double sum{
std::accumulate(
vec.begin(),
vec.end(),
0.0
)
};
return sum / vec.size();
}
static void test_concepts_requires_04()
{
std::vector<int> numbers{ 1, 2, 3, 4, 5 };
std::vector<double> moreNumbers{ 1, 2, 3, 4, 5 };
auto average{ avg(numbers) };
// the associated constraints are not satisfied:
// auto anotherAverage{ avg(moreNumbers) };
}
static void test_concepts_requires_basic_usage()
{
test_concepts_requires_01();
test_concepts_requires_02();
test_concepts_requires_03();
test_concepts_requires_04();
}
}
namespace Trailing_Requires_Clause {
template <typename T>
auto add(T a, T b) requires Numerical<T>
{
return a + b;
}
template <typename T>
auto addEx(T a, T b) requires std::integral<T> or std::floating_point<T>
{
return a + b;
}
template <typename T, typename U>
auto add(T a, U b) requires Numerical<T> and Numerical<U>
{
return a + b;
}
static void test_trailing_requires_clause()
{
auto sum1{ add(123.456f, 654.321f) };
std::cout << sum1 << std::endl;
auto sum2{ add(123.456, 654.321) };
std::cout << sum2 << std::endl;
auto sum3{ add(123.456, 654.321F) };
std::cout << sum3 << std::endl;
}
}
namespace Constrained_Template_Parameters {
template <Numerical T>
auto add(T a, T b)
{
return a + b;
}
template <Numerical T, Numerical U>
auto add(T a, U b)
{
return a + b;
}
static void test_constrained_template_parameters()
{
auto sum1{ add(123.456f, 654.321f) };
std::cout << sum1 << std::endl;
auto sum2{ add(123.456, 654.321) };
std::cout << sum2 << std::endl;
auto sum3{ add(123.456, 654.321F) };
std::cout << sum3 << std::endl;
}
}
namespace Abbreviated_Function_Templates {
static auto add(Numerical auto a, Numerical auto b)
{
return a + b;
}
static void test_abbreviated_function_template_syntax()
{
auto sum1{ add(123.456f, 654.321f) };
std::cout << sum1 << std::endl;
auto sum2{ add(123.456, 654.321) };
std::cout << sum2 << std::endl;
auto sum3{ add(123.456, 654.321F) };
std::cout << sum3 << std::endl;
}
}
namespace UserDefined_Concept {
template<typename T>
constexpr bool isGreaterThanWord{ sizeof(T) > 2 };
// using <type_traits>
template <typename T>
concept GreatIntegral = std::is_integral<T>::value and isGreaterThanWord<T>;
template<GreatIntegral T>
T incrementByOne(const T& arg) {
return arg + 1;
}
auto incrementByTwo(GreatIntegral auto arg) {
return arg + 2;
}
static void test_user_defined_concept()
{
auto n{ 123 };
n = incrementByOne(n);
// short s{ 1 };
// the associated constraints are not satisfied:
// s = incrementByOne(s);
n = incrementByTwo(n);
// short s{ 1 };
// the associated constraints are not satisfied:
// s = incrementByTwo(s);
}
}
void main_concepts_requires_functions()
{
Requires_Clause::test_concepts_requires_basic_usage();
Trailing_Requires_Clause::test_trailing_requires_clause();
Constrained_Template_Parameters::test_constrained_template_parameters();
Abbreviated_Function_Templates::test_abbreviated_function_template_syntax();
UserDefined_Concept::test_user_defined_concept();
}
// =====================================================================================
// End-of-File
// =====================================================================================