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
4
votes
2 answers

Interaction between templates and overloads

The output of the following code is TA, but I don't understand why. #include #include struct A {}; template void fun(T) { std::cout << "T"; } template void caller(T t) { fun(A{}); …
4
votes
2 answers

Name lookup in template base: why do we add this->

Consider the following: struct Base { void foo(); }; template struct Derived : Base { void bar() { this->foo(); } }; Typically, we explain that this-> makes foo() a dependent name, so its lookup is posponed to the…
4
votes
1 answer

What is the right way to define a friend function outside a template class?

If I have a normal class I can "inject" a non-free friend function inside the class. (That among other things can be only be found by ADL). case 1: class A{ double p_; friend double f(A const& a){return a.p_;} }; If instead this is a template…
alfC
  • 14,261
  • 4
  • 67
  • 118
4
votes
0 answers

Is #include importing symbols to the global namespace?

Let's consider such code: #include int main() { vector v = {1, 2, 3}; return 0; } GCC 6.3 doesn't compile it with error: ‘vector’ was not declared in this scope - great, I'd expect that. I need to write std::vector to…
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
4
votes
1 answer

Does Argument-Dependent Lookup go before normal scope lookup?

This is the code in question that appears in §13.3 of "C++ Primer", 5ed: void swap(Foo &lhs, Foo &rhs) { using std::swap; swap(lhs.h, rhs.h); // uses the HasPtr version of swap // swap other members of type Foo } The book mentions the…
4
votes
4 answers

How avoid std naming conflicts due to Koenig lookup

As a learning exercise I've been reimplementing some of the STL algorithm. Even though I've not added any using directives or using declarations for the std namespace my test code won't compile unless I explicitly prefix those functions shadowing…
Daniel Näslund
  • 2,300
  • 3
  • 22
  • 27
4
votes
3 answers

c++ ordinary lookup vs argument dependent lookup

Considering this sample described in http://en.cppreference.com/w/cpp/language/adl: namespace A { struct X; struct Y; void f(int); void g(X); } namespace B { void f(int i) { f(i); // calls B::f (endless…
FlashMcQueen
  • 635
  • 3
  • 13
4
votes
1 answer

How does a using-declaration reduce the names available for lookup without ADL?

#include #include class X {}; namespace N { std::string to_string(X) { return "foo"; } void foo() { //using std::to_string; // will break the build if uncommented... //using N::to_string; // ...unless this is…
gd1
  • 11,300
  • 7
  • 49
  • 88
4
votes
1 answer

Nested Classes and ADL

Here's the code: namespace Namespace { struct L0 { enum SomeEnum { EnumVal }; struct L1 { friend void f(SomeEnum) { std::cout << "f()" <<…
dsi
  • 586
  • 3
  • 13
4
votes
2 answers

argument dependent lookup not considered

Why argument dependent lookup doesn't consider Foo::dynamicCast, shouldn't it consider namespace Foo because the Base class is in this namespace? #include using namespace std; namespace Foo { template
José
  • 3,041
  • 8
  • 37
  • 58
4
votes
0 answers

C++ non-qualified lookup

I have the following code: //mystd plays a role of the std namespace //which does not allow any new overloads in it //so we define one in the global namespace namespace mystd { template struct A {}; } //if one uncomment this,…
4
votes
2 answers

initializer_list and argument-dependent lookup

I'm trying to use an std::initializer_list as an argument in a function that uses argument-dependent lookup (ADL). But I don't get it to work and I don't understand why. The following is a minimal failing example: #include
Joel
  • 1,295
  • 15
  • 30
4
votes
1 answer

Function name resolution depending on template parameter

Came across the following task in a test: #include using namespace std; template void adl(T) { cout << "T"; } struct S { }; template void call_adl(T t) { adl(S()); adl(t); } void adl(S) { cout << "S";…
noname7619
  • 3,370
  • 3
  • 21
  • 26
4
votes
1 answer

Best match not found by ADL after point of instantiation. Is this UB?

Consider the following code, in which the location of the overloads of f causes some non-intuitive behaviour. The code compiles with no warnings in both Clang 3.4.1 and gcc 4.8. template struct A { static const int value =…
4
votes
1 answer

Which namespace does contain the declaration of this friend function?

The friend function below is not found by ordinary lookup (§7.3.1.2/3), but is found by ADL (§3.4.2/4 second bullet point), so the code compiles and executes normally (live example). But the function f isn't declared in any namespace. For example,…
Wake up Brazil
  • 3,421
  • 12
  • 19