-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP739.Max.cpp
More file actions
63 lines (56 loc) · 1.13 KB
/
P739.Max.cpp
File metadata and controls
63 lines (56 loc) · 1.13 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
#include <iostream>
#include <vector>
#include <concepts>
#include <typeinfo>
class X
{
public:
std::string str;
int i;
bool operator<(const X& x)
{
return str < x.str;
}
};
class Y
{
public:
std::string str;
int i;
bool operator>(const Y& x)
{
return str > x.str;
}
};
template<typename T>
concept LessThanComparable = requires(T x, T y)
{
{ x < y } -> std::same_as<bool>;
};
template<typename T>
concept GreaterThanComparable = requires(T x, T y)
{
{ x > y } -> std::same_as<bool>;
};
template<typename T>
T max(T a, T b) requires LessThanComparable<T>
{
std::cout << "max with LessThanComparable constraint" << std::endl;
return b < a ? a : b;
}
template<typename T>
T max(T a, T b) requires GreaterThanComparable<T>
{
std::cout << "max with GreaterThanComparable constraint" << std::endl;
return a > b ? a : b;
}
int main(int argc, char const *argv[])
{
X x1, x2;
max(x1, x2);
Y y1, y2;
max(y1, y2);
std::cout << typeid(max<X>).name() << std::endl; // F1XS_S_E
std::cout << typeid(max<Y>).name() << std::endl; // F1YS_S_E
return 0;
}