Questions tagged [argument-dependent-lookup]

A form of name lookup in C++ which allows function names to be found in namespaces associated with the arguments used in the function call.

An unqualified function call such as func(a, b, c) will lookup the name func in the namespaces that are associated with the types of the arguments a, b and c. For example, if a has type ns::A and a function ns::func exists then it can be found by argument dependent lookup and will be added to the overload set used to resolve the call.

The reason for this feature is to allow overloaded operators declared in namespaces to be found, because operators cannot be qualified by a namespace e.g.

namespace ns
{
  struct A { };

  A operator+(const A&, const A&);
}

void f(ns::A a1, ns::A a2)
{
  ns::A sum = a1 + a2;   // must find ns::operator+
}

In the example above the + operator must find ns::operator+, but without ADL it would not.
ADL allows the natural a1 + a2 syntax to work as expected, instead of having to write something like a2 ns::+ a2, which isn't valid syntax, or ns::operator+(a1, a2).

ADL is sometimes known as "Koenig Lookup" after Andrew Koenig, who suggested the feature.

367 questions
214
votes
4 answers

What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")?

What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well. Preferably I'd like to know: Why is it a good thing? Why is it a bad thing? How does it work?
user965369
  • 5,413
  • 13
  • 33
  • 47
87
votes
4 answers

Why doesn't ADL find function templates?

What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; …
Hugh
  • 8,872
  • 2
  • 37
  • 42
81
votes
6 answers

Is Bjarne wrong about this example of ADL, or do I have a compiler bug?

I'm reading The C++ Programming Language, 4th Edition (by Bjarne Stroustrup) about argument-dependent-lookup. Here is the quote (26.3.6, Overaggressive ADL): Argument-dependent lookup (often referred to as ADL) is very useful to avoid verbosity…
maverik
  • 5,508
  • 3
  • 35
  • 55
61
votes
2 answers

What are the pitfalls of ADL?

Some time ago I read an article that explained several pitfalls of argument dependent lookup, but I cannot find it anymore. It was about gaining access to things that you should not have access to or something like that. So I thought I'd ask here:…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
61
votes
1 answer

Should custom containers have free begin/end functions?

When creating a custom container class that plays by the usual rules (i.e. works with STL algorithms, works with well-behaved generic code, etc.), in C++03 it was sufficient to implement iterator support and member begin/end functions. C++11…
zeuxcg
  • 9,216
  • 1
  • 26
  • 33
46
votes
4 answers

How do I write an ADL-enabled trailing return type, or noexcept specification?

Imagine I'm writing some container template or something. And the time comes to specialize std::swap for it. As a good citizen, I'll enable ADL by doing something like this: template void swap(my_template& x, my_template& y) { …
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
43
votes
3 answers

what does `using std::swap` inside the body of a class method implementation mean?

I was trying to learn and adopt the copy-swap idiom following this thorough explanation on this question: the Copy-Swap Idiom. But I found some code I had never seen: using std::swap; // allow ADL in this example class dumb_array { public: //…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
41
votes
1 answer

C++ compile time counters, revisited

TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the solution into a fameta::counter class that solves a…
39
votes
3 answers

Is it okay to define a totally general swap() function?

The following snippet: #include #include namespace foo { template void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } struct bar {…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
39
votes
2 answers

What's the point of iter_swap?

I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap, and of course it only uses swap instead of std::swap…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
33
votes
3 answers

Interesting behavior of compiler with namespaces

Assume the following code: #include using namespace std; namespace X { class A{}; void f(A a){} void g(int a){} } int main() { X::A a; f(a); g(5); } When I compile the code, the following compile error…
29
votes
2 answers

How does "using std::swap" enable ADL?

In What is the copy-and-swap idiom this example is shown: friend void swap(dumb_array& first, dumb_array& second) // nothrow { // enable ADL (not necessary in our case, but good practice) using std::swap; // by swapping the members of…
template boy
  • 10,230
  • 8
  • 61
  • 97
26
votes
2 answers

Why Argument Dependent Lookup doesn't work with function template dynamic_pointer_cast

Consider the following C++ program: #include struct A {}; struct B : A {}; int main() { auto x = std::make_shared(); if (auto p = dynamic_pointer_cast(x)); } When compiling with MSVC 2010, I obtain the following…
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
25
votes
3 answers

Is ADL the only way to call a friend inline function?

Let us define f, as a friend function of S, inside the declaration of S: struct S { friend void f() {} }; I cannot find a way to call f. Is it true, then, that such an inline friend function can only be called with argument-dependant…
YSC
  • 38,212
  • 9
  • 96
  • 149
23
votes
2 answers

getting an element from a tuple

Possible Duplicate: Why doesn't ADL find function templates? Calling get does not seem to invoke argument dependent lookup: auto t = std::make_tuple(false, false, true); bool a = get<0>(t); // error bool b = std::get<0>(t); // okay g++…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
1
2 3
24 25