Debian Codegraph @

yo/advancedtemplates/concepts/examples/simple.cc

raw
1 #include <string>
2 #include <map>
3 #include <iostream>
4
5 //addable
6 template <typename Type>
7 concept Addable =
8 requires(Type par)
9 {
10 ++par; // `Type' variables must support
11 par++; // pre-/postfix increment
12 }
13 and requires(Type lhs, Type rhs) // and also addition
14 {
15 lhs + rhs;
16 };
17 //=
18
19 // typename Type::value_type;
20 //index
21 template <typename Type>
22 concept HasIndex =
23 requires(Type tp)
24 {
25 tp[0];
26 };
27
28 template <HasIndex Type>
29 auto idx(Type const &obj, size_t idx)
30 {
31 return obj[idx];
32 }
33 //=
34
35 template <typename Type>
36 concept HasStrIndex =
37 requires(Type tp)
38 {
39 tp[std::string{}];
40 };
41
42 template <HasStrIndex Type>
43 auto value(Type &obj, std::string const &key)
44 {
45 return obj[key];
46 }
47
48 using namespace std;
49
50 int main()
51 {
52 // idx(1, 2);
53 string str;
54 cout << idx(str, 1) << endl;
55
56 map<string, double> msd;
57 value(msd, "hi");
58 }