Debian Codegraph @

yo/advancedtemplates/concepts/examples/specializations.cc

raw
1 #include <iostream>
2 #include <vector>
3
4 template <typename Type>
5 concept Addable =
6 requires(Type lh, Type rh)
7 {
8 lh + rh;
9 };
10
11 //generic
12 template <typename Tp>
13 struct Handler
14 {
15 Handler()
16 {
17 std::cout << "Generic Handler\n";
18 }
19 };
20 //=
21
22 //addable
23 template <Addable Tp> // constrain Tp to addable types
24 struct Handler<Tp>
25 {
26 Handler()
27 {
28 std::cout << "Handler for types supporting operator+\n";
29 }
30 };
31 //=
32
33 //use
34 int main()
35 {
36 Handler<std::vector<int>>{}; // generic
37 Handler<int>{}; // specialized
38 }
39 //=