cpp-test-koenig.cc
//
// simple test of ADL (Koenig lookup)
//
#include <iostream>
using namespace std;
namespace A {
struct S { // class, all public
int data;
S(int d = 0): data(d) {} // implicit ctr
// copy ctr created by default
// operator = created by default
// dtr created by default
void m(); // undefined - no problem if not used
};
void f(int i, const S &a) {
cout << "A::f(i,S)\n";
}
void f() {
cout << "A::f()\n";
}
S operator + (S s1, S s2) {
cout << "A::operator+(S,S)\n";
return s1.data+s2.data;
}
} // namespace A
// macro for testing
#define TEST(command) cout << ">>>>> " << #command << ":\n "; command
int main() {
A::S s1(1), s2(2), s3;
TEST(s3 = s1 + s2); // output: A::operator+(S,S)
// operator+ found in the namespace where type of s1,s2 is defined
TEST(f(5, s1)); // output: A::f(i,S)
// f(int i, S&a) found in the namespace where type of s1 is defined
TEST(A::f()); // output: A::f()
// f() should be qualified because of no arguments
}