minimum_concepts.cpp
921 Bytes
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
#include <iostream>
#include <string>
#include <concepts>
using namespace std;
template <class T>
concept LessComparable = requires(const T l, const T r, bool res)
{
res = (l < r);
};
template <class T> T minimum(T x, T y) requires std::constructible_from<T, const T&> && LessComparable<T>
{
if (x < y)
return x;
else
return y;
}
class NotComparable
{
};
class NotCopyConstructible
{
public:
NotCopyConstructible(){};
NotCopyConstructible(const NotCopyConstructible&) = delete;
friend bool operator<(NotCopyConstructible, NotCopyConstructible){return true;};
};
int main()
{
int x = 50, y = 30;
string a = "hello", b = "goodbye";
cout << "minimum for ints " << minimum(x, y) << endl;
cout << "minimum for strings " << minimum(a, b) << endl;
// minimum(NotComparable(), NotComparable());
// minimum(NotCopyConstructible(), NotCopyConstructible());
}