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
3
votes
1 answer

Operator overloading, name resolution and namespaces

I would like some light to be shed on a puzzling situation involving ADL, namespaces and operator overloading. Let Foo be a library which defines a class ( Deriv) in its own namespace, along with a templated operator * which returns another…
Louen
  • 3,617
  • 1
  • 29
  • 49
3
votes
1 answer

Defining a function in a namespace other than the ADL, "local" or global namespace

See the code below: #include /// Definition of void perform(a_lib::a_class&). Where should I put this definition? /// See the comments below for where I've tried placing it. // void perform(a_lib::a_class&) { // std::cout <<…
haisareisa
  • 33
  • 4
3
votes
1 answer

Name hiding by using declaration

#include struct H { void swap(H &rhs); }; void swap(H &, H &) { std::cout << "swap(H &t1, H &t2)" << std::endl; } void H::swap(H &rhs) { using std::swap; swap(*this, rhs); } int main(void) { H a; H b; …
3
votes
1 answer

C++ using declaration and argument dependent lookup

Is the code below valid C++98 or does it require a newer version of the C++ standard? namespace basic { void f(int) {} } namespace lib { template void g(T1 x1, T2 x2) { using basic::f; // pull in function f for basic…
3
votes
4 answers

Why/when should we prefer using std::swap; swap(a, b) over std::iter_swap(&a, &b)?

Why/when should we prefer using std::swap; swap(a, b); over std::iter_swap(&a, &b)?
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
1 answer

ADL related GCC 4.7.2 issue with expression SFINAE

Take the following code, which is characterized by Reliance on ADL for a specific behavior (volume) Using decltype for return type and relying on SFINAE to discard extra overloads namespace Nature { struct Plant {}; double volume(Plant){…
Nick
  • 931
  • 6
  • 17
3
votes
2 answers

Why is Koening lookup not working here?

Argument dependent lookup says: For arguments of class type (including union), the set consists of... a) The class itself b)... Then why can't printX find X? #include using namespace std; class A { public: static const int X =…
madu
  • 5,232
  • 14
  • 56
  • 96
3
votes
3 answers

Getting constexpr functions resolved without creating parameter objects

Short version: If I have function like: constexpr bool has_some_property(Foo) { return true; } Is there any way to invoke the function without having to actually instantiate Foo? Say if Foo is not default constructible? Long winded…
Jay Miller
  • 2,184
  • 12
  • 11
3
votes
3 answers

why is overload preferred to explicit specialization in ADL

Consider the code: #include #include // std::swap C++98 #include // std::swap C++11 namespace A { template struct Foo {}; template void swap(Foo &lhs, Foo &rhs) { std::cout <<…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
1 answer

Customization points and ADL

I am writing a library and there is a function that performs an (unqualified) call to free function foo using an arbitrary type as argument: namespace lib { template auto libfunc(T && t) { return foo(std::forward(t)); } } //…
MadScientist
  • 3,390
  • 15
  • 19
3
votes
1 answer

friend template argument-dependent lookup

It'n known that friend function defined in class scope can be found via argument-dependent lookup so we have to use class type in friend function type, but if we define friend function outside of class it function parameters can be left empty. So…
3
votes
3 answers

Simulating argument-dependent lookup for template arguments

I've encountered this problem while writing some library-like code recently, and I thought discussing it might help others as well. Suppose I have a library with some function templates defined in a namespace. The function templates work on types…
bogdan
  • 9,229
  • 2
  • 33
  • 48
3
votes
1 answer

Why function fun is not qualified for ADL?

I have simple namespace, which has one variable and one function. In main I try to call function without namespace qualifier, and variable with namespace qualifier. namespace SAM { int p = 10; void fun(int) { cout<<"Fun gets…
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
3
votes
1 answer

Are there different rules regarding ADL or naming clashes with regard to overloaded operators?

I think this example best illustrates my question: namespace N { class C { public: friend bool operator==(const C& c, const C& x) { return true; } friend bool f(const C& c, const C& x) { …
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
3
votes
2 answers

When is ADL applied?

There are 3 examples: I. typedef int foo; namespace B { struct S { operator int(){ return 24; } }; int foo(B::S s){ return 0; } } int main() { int t=foo(B::S()); //24, ADL does not apply } II. namespace B { …
St.Antario
  • 26,175
  • 41
  • 130
  • 318