How does this work? Is it 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 somebody tell me why i can’t use something like
f(A<int>());
Indeed works because of Argument dependent lookup/Koenig lookup(ADL)
Koenig Lookup states:
Consider a simplistic example not using templates and it should help you understand ADL at work better:
Output:
When you use
f(A<int>()), it mandates thatf()requires an argument of the typeint, but your structure does not provide any conversion fromAtointand hence the error.If you provide the appropriate conversion then it will work as well. Something like: