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

namespace, class member ADL conflict

#include namespace outside { struct A { int outer = 42; friend void print(A const& a, std::ostream& os) { os << "outside::A " << a.outer << '\n'; } }; namespace inside { struct A : outside::A { int inner…
dpj
  • 933
  • 1
  • 7
  • 14
0
votes
4 answers

Experience with Architecture Description Languages

Being familiar with graphical modeling tools, I recently thought about the concept of architecture description languages (ADL) where one describes architectures in a textual form in order to comprehensively document it. I see advantages in this…
Bernd
  • 3,390
  • 2
  • 23
  • 31
0
votes
5 answers

In what situations Argument Dependent name Look-up (ADL) kicks in?

In Wikipedia article below quote is mentioned: ADL only occurs if the normal lookup of an unqualified name fails to find a matching class member function. In this case, other namespaces not considered during normal lookup may be searched where…
iammilind
  • 68,093
  • 33
  • 169
  • 336
0
votes
1 answer

Calling a function by ADL from another function

I have a question relating to how types are found by ADL in generic situations. Specifically, I have some 'generic' code where I need to check at compile-time for the presence of a function which should be found by ADL. For example: #include…
TPJ
  • 179
  • 1
  • 11
0
votes
1 answer

Usual unqualified lookup and Argument-dependent name lookup(ADL)

For unqualified name lookup, 'Usual unqualified lookup' and 'Argument-dependent name lookup'(ADL), I cannot find in standard which one happens first ? Again as both trying to add something to the overload candidate set, the order doesn't seems to…
RoundPi
  • 5,819
  • 7
  • 49
  • 75
-2
votes
1 answer

Why name lookup can't find std::swap when a base class has a swap function in C++?

I have a replica of what's inside my code #include using namespace std; class Parent { public: void swap(Parent*); }; class A : public Parent { public: void handle(); }; void A::handle() { long x = 1; …
Shady Atef
  • 2,121
  • 1
  • 22
  • 40
-3
votes
2 answers

Trying to create a universal map printer and this snippet won't compile (Issue with ADL?)

Here is the full program. I'm using clang 13 with -std=c++20 -stdlib=libc++. The error message, not included here because it's extremely long, basically says that the compiler does not even consider the operator<< function I've included here (error…
1 2 3
24
25