Skip to content

Commit 98f84e9

Browse files
committed
Simple ADL example.
1 parent c23e36d commit 98f84e9

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

code_examples/theory/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ install(TARGETS initializer_list DESTINATION bin)
1515

1616
add_executable(adl_unqalified adl_unqalified.cpp)
1717
# Compile only
18+
19+
add_executable(adl_simple adl_simple.cpp)
20+
# Compile only

code_examples/theory/adl_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ std::ostream& operator << (std::ostream& s, const X&) {
1010
void calling_site() {
1111
Custom::X x;
1212
Custom::operator << (std::cout, x); // OK, explicit call
13-
std::cout << x; // OK, unqualified operator <<, ADL
13+
std::cout << x; // Requires ADL
1414
}

code_examples/theory/adl_simple.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <iostream>
2+
#include "adl_simple_code.h"
3+
int main() {
4+
calling_site();
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace A {
2+
struct X {};
3+
void some_func(const X&) {}
4+
}
5+
6+
namespace B {
7+
struct Y {};
8+
void some_func(const Y&) {}
9+
}
10+
11+
void some_func(const auto&) {}
12+
13+
void calling_site() {
14+
A::X x; B::Y y;
15+
some_func(x); // Calls A::some_func
16+
some_func(y); // Calls B::some_func
17+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
#include "adl_unqalified_code.h"
2-
int main() {}
2+
int main() {
3+
A::B::calling_site();
4+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
namespace A {
22
void some_call(int) {}
3+
void some_call(const char*) {}
34
namespace B {
45
void some_call(double) {}
56

6-
void test() {
7+
void calling_site() {
78
some_call(1); // A::B::some_call
89
some_call(2.0); // A::B::some_call
10+
//some_call("hello world"); Will not compile, no conversion from const char* -> double
911
}
1012
}
1113
}

0 commit comments

Comments
 (0)