-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcepts.cpp
104 lines (73 loc) · 2.44 KB
/
concepts.cpp
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
#include <iostream>
#include <concepts>
#include <string>
#include <vector>
#include <numeric>
// a very primitive concept declaration
template <class T>
concept integral = std::is_integral_v<T>;
// a concept declaration that also specifies the return type.
template<typename T>
concept Hashable = requires(T a)
{
{ std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
};
// There are 4 ways to write *Constrained* C++20 function templates:
// first
template<Hashable T>
void f1(T) {
}
// second
template<typename T>
requires Hashable<T>
void f2(T) {
}
// third. second and third are very similar. It is just that the third version requires clause as the last part of a function declaration.
template<typename T>
void f3(T) requires Hashable<T> {
}
// fourth. I think this is the most concise way, and should be the default method to use the constrained function templates.
void f4(Hashable auto t) {
}
struct NotHashable {
};
template<typename T>
concept has_string_data_member = requires(T v) {
{ v.name_ } -> std::convertible_to<std::string>;
};
struct Person {
int age_ { 0 };
std::string name_;
};
struct Box {
double weight_ { 0.0 };
double volume_ { 0.0 };
};
// The above concept restricts an “interface” for basic clocks. We require that it has the three member functions, but we **don’t** specify what type do they return.
template <typename T>
concept Clock = requires(T c) {
c.start();
c.stop();
c.getTime();
};
// std::integral and std::floating_point are concepts!
template <typename T>
requires std::integral<T> || std::floating_point<T>
constexpr double average(const std::vector<T> &vec) {
const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);
return sum / vec.size();
}
int main()
{
f1(std::string("abc")); // Compiles.
f2(std::string("abc")); // Compiles.
f3(std::string("abc")); // Compiles.
f4(std::string("abc")); // Compiles.
// f1(NotHashable{}); // Error: "NotHashable" does not satisfy "Hashable" concept.
static_assert(has_string_data_member<Person>);
static_assert(!has_string_data_member<Box>);
std::vector ints { 1, 2, 3, 4, 5};
std::cout << average(ints) << '\n';
// std::vector strings {"abc", "xyz"};
// auto test = average(strings); // compile error: required by the constraints of ‘template<class T> requires (integral<T>) || (floating_point<T>) constexpr double average(const std::vector<T>&)’
}