How it works? Is this related to ADL?
#include <iostream> template <typename T> struct A { friend void f(T x) { std::cout << "A\n"; } }; int main() { f(new A<void*>()); }
Can someone tell me why I can't use something like
f(A<int>());
f(new A<void*>());
Really works due to Koenig argument / search dependent ( ADL ) lookupIn Koenig search mode:
You do not need to qualify a namespace (scope) for functions if one or more argument types are defined in the function namespace.
Consider a simplified example that doesn't use templates, and should help you better understand how ADL works:
#include <iostream> struct A { friend void f(A x) { std::cout << "A\n"; } }; int main() { f(A()); return 0; }
Exit:
A
When you use f(A<int>()) , it requires f() require an argument of type int , but your structure does not provide any conversion from A to int and therefore errors.
f(A<int>())
f()
int
If you provide the appropriate conversion, it will also work. Something like :
operator int(){return 1;}