0

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"?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • @ybungalobill: Why? It seems pretty similar to what's [here](http://en.wikipedia.org/wiki/Argument-dependent_name_lookup#Criticism)... where's the mistake? – user541686 Feb 12 '12 at 05:48
  • You can't take a piece of code out of context, add yet another piece of code and assume it has the same meaning. ADL is behavioral feature, not syntactic one. I.e. you have to say what `foo`s are declared in your program to determine whether ADL is used in the unqualified version or not. – Yakov Galka Feb 12 '12 at 05:56

1 Answers1

1

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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Hmm.. wait but what about something like `swap`, where it's defined in both the std namespace and in the class? Wouldn't that require a `using std::swap;`? Where is the mistake in my example? (Mine looks a lot like what's [here](http://en.wikipedia.org/wiki/Argument-dependent_name_lookup#Criticism).) – user541686 Feb 12 '12 at 05:47
  • In your example, the only usable `foo` is the one brought in from `some_namespace`, so there is no `foo` to be found through ADL. – James McNellis Feb 12 '12 at 05:50
  • @Mehrdad: The code you posted in question compares a qualified versus unqualified call. While it's true the the first may involve ADL, it's still not "The ADL". Yes, it's common to use `using` together with ADL, but you can't compare it with qualified calls because it is a completely different feature. – Yakov Galka Feb 12 '12 at 05:51
  • @JamesMcNellis: I think you missed my point. The point is that the person who **defines** `my_type` *may or may not* define the function. It's not my choice whether the client defines that function or not. So my question was whether I should *allow* that "by default" or not -- but *obviously* if there is no such function, then it won't be found via ADL... – user541686 Feb 12 '12 at 06:00
  • Please post _complete examples_ that _clearly demonstrate_ what you are asking. Nothing in your question demonstrates what you describe in your comment. In any case, the answer is no different: avoid ADL except in the specific cases where it is useful. – James McNellis Feb 12 '12 at 06:06