Pregunta

How do I tell whether I should use

my_type bar;
using some_namespace::foo;
foo(bar);

instead of

some_namespace::foo(bar);

when calling my function foo (that is not within my immediate scope)? Is there a generic "rule" for figuring out whether you should use ADL or not? Which one should I use "by default"?

¿Fue útil?

Solución

That is not ADL. In both of your examples, foo is found via normal lookup. An example using ADL would be as follows:

namespace ns {
    class A { };
    void f(A) { };
}

int main() {
    f(A());
}

Here, f is not found via normal lookup, but it is found via argument-dependent lookup (because it is in namespace ns alongside A). In any case...

Avoid ADL wherever possible.

ADL is beneficial in certain, specific scenarios, for example for operator overloading and for the swappable concept. However, it should be used sparingly, as it leads to bizarre, unexpected behavior in many other cases.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top