Debian Codegraph @

yo/advancedtemplates/concepts/examples/bounditer/main.cc

raw
1 #include <string>
2 #include <compare>
3
4 //string
5 template <typename Data>
6 struct String
7 {
8 struct iterator
9 {
10 using value_type = Data;
11
12 std::string::iterator d_iter;
13
14 // Note the <>: operator== is a function template
15 // specialization as 'iterator' is a class template
16 friend bool operator==<>(iterator const &lhs, iterator const &rhs);
17 };
18 iterator begin()
19 {
20 return iterator{};
21 }
22 };
23 //=
24
25 // The following concept checks that Type is Storage<Data>::iterator, pretty
26 // much like a function template with parameter Storage<Data>::iterator would
27 // do. We can't do the latter, but concept-defined types can be used. The
28 // concept also makes is superfluous to define specific types in the nested
29 // class which are used to select the matching free operator. The
30 // concept-based approach cannot accidentally be broken by the user defining
31 // identical specific types as the concepts approach simply uses the
32 // templates' types themselves.
33
34 //concept
35 template<typename Type>
36 concept StringIterator =
37 std::same_as<Type,
38 typename String<typename Type::value_type>::iterator>;
39 //=
40
41 //operator
42 inline bool operator==(StringIterator auto const &lhs,
43 StringIterator auto const &rhs)
44 {
45 return lhs.d_iter == rhs.d_iter;
46 }
47 //=
48
49 // template <StringIterator Param>
50 // inline bool operator==(Param const &lhs,
51 // Param const &rhs)
52 // {
53 // return lhs.d_iter == rhs.d_iter;
54 // }
55
56 int main()
57 {
58 String<int> str;
59 return str.begin() == str.begin();
60 }