-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_inheritance.cpp
More file actions
56 lines (41 loc) · 857 Bytes
/
template_inheritance.cpp
File metadata and controls
56 lines (41 loc) · 857 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
template <class T, class U>
struct Holder: T, U {
using T::operator();
using U::operator();
};
struct A {
void operator()(const A&) {
std::cout << "A" << std::endl;
}
};
struct B {
void operator()(const B&) {
std::cout << "B" << std::endl;
}
};
template <class T>
struct Comparable {
friend bool operator==(T const& l, T const& r) {
return l.equal_to(r);
}
friend bool operator!=(T const& l, T const& r) {
return !l.equal_to(r);
}
};
struct User: private Comparable<User> {
bool equal_to(const User& other) const {
return id == other.id;
}
User(int id): id(id){}
int id;
};
int main() {
Holder<A,B> h;
h(A());
h(B());
User u1 = {0};
User u2 = {2};
std::cout << (u1 == u2) << std::endl;
return 0;
}