-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtwo_phase_lookup.cpp
More file actions
40 lines (32 loc) · 832 Bytes
/
Copy pathtwo_phase_lookup.cpp
File metadata and controls
40 lines (32 loc) · 832 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
// Two-phase lookup
#include <iostream>
namespace A {
class MyInt {
public:
MyInt(int) {}
};
MyInt operator-(MyInt const&) { return MyInt{0}; }
bool operator>(MyInt const&, MyInt const&) { return false; }
// replacing MyInt with int won't work,
// as int does not have an associated namespace to use in ADL
typedef MyInt Int;
} // namespace A
template <typename T>
void f(T i) {
if (i > 0) {
// found by ADL at POI second-phase lookup, no need for forward
// declaration
g(-i);
}
}
int main() {
A::MyInt m(42);
f(m);
}
namespace A {
// this is NOT found at second-phase lookup, since only ADL is performed
void g() {}
// found by ADL at POI second-phase lookup, no need for forward declaration
void g(Int) { f<Int>(42); }
// Point of instantiation (POI) is here
} // namespace A