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

How to document friend injection functionality with doxygen?

Given some code like in the following example: class MyClass; class Injector { /** * @brief MyClass addition. */ friend MyClass operator+(MyClass a, MyClass b) { ... } }; class MyClass: private Injector {}; How can I make…
4
votes
1 answer

how do I call an inline friend function with the same name as a member function?

As described here C++11 style SFINAE and function visibility on template instantiation class member functions overshadow free functions. Using a fully qualified name usually works, however I am having a hard time with friend functions of other…
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
4
votes
3 answers

ADL in constructor initialization list

How can I enable ADL in a constructor initialization list? For example, let's say that I have a bignum that has a namespace-level abs function. Now I want to write a class Foo that initializes its members with the absolute values of the instances…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
4
votes
0 answers

Getting "invalid application descriptor: Unknown namespace: library://ns.adobe.com/flex/spark " in adl (flash builder command-line debugger)

Getting "invalid application descriptor: Unknown namespace: library://ns.adobe.com/flex/spark" when using adl (command-line debugger) for a flash-builder 4.6 adobe air project its a very simple project mxml file starts like this :
3
votes
2 answers

Function template declaration order affects visibility (sometimes)

I'm trying to create a function: template void doIt( T*& p ) { if ( !p ) { return; } T& ref = *p; getClassName( ref ); } where the behavior varies according to the type of p passed in. In particular, the version of…
user48956
  • 14,850
  • 19
  • 93
  • 154
3
votes
1 answer

Why does "xxx::function();" not work but "using namespace xxx; function();" does?

I am using Boost's graph C++ library, and I stumbled over a problem when asking for the number of vertices of a grid graph. The following code snipped creates a 2-dimensional grid graph of shape 5 by 6, and then prints the number of vertices of that…
3
votes
0 answers

Does argument-dependent lookup also include overload set in inaccessible bases?

Here's a simple example: template struct t1 : protected T { }; template struct t2 { template struct inner : t1 { private: template requires(Index == I) …
3
votes
1 answer

Trouble with using overloaded << for std::variant

I have an overloaded << for an aliased std::variant (A::Var). I also have a templated function defined in a class in a different namespace C::Wrapper which just forwards its argument to an std::ostream. I am trying to invoke it from another function…
Albert
  • 337
  • 1
  • 10
3
votes
0 answers

Why does std::sort() without prefix "std" not compile in Visual Studio 2010 when using a lambda?

I have encountered a situation where a call to std::sort() works without explicitly writing the namespace. According to Using std::sort() without prefix "std" and also without "using namespace std;" compiles successfully, the compiler uses ADL and…
Fabian
  • 4,001
  • 4
  • 28
  • 59
3
votes
0 answers

When to use specifically the niebloid std::ranges::construct_at instead of std::construct_at?

In the situation that we have two choices of exactly the same functionality. One is a niebloid, the other is a function. Are there any guidelines for this in general? std::construct_at -…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
3
votes
2 answers

Deduction of template arguments for friend function declared in class template

Consider the following example: #include template struct S { friend int Func(T) // decl-1 { return V; } }; struct U { friend int Func(U); // decl-2 }; template struct S; // spec-1 int…
3
votes
0 answers

Implementation-specific questions about type erasure via external polymorphism

I have recently watched a CppCon 2021 talk by Klaus Iglberger about type erasure pattern and tried to implement it based on the slides. Despite the code from slides not compiling, after a lot of searching and reading SO answers I got an…
Ave Milia
  • 599
  • 4
  • 12
3
votes
1 answer

Warn against missing std:: prefixes due to ADL

It is possible to omit the std:: namespace for s when the argument types are in that namespace, which is usually the case. Is there any warning or clang-tidy rule that finds such omissions? #include #include…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
3
votes
1 answer

customisation point for alias to std types

Let's say I am writing some generic algorithm in lib namespace that calls a customisation point my_func. First attempt is using ADL for my_func one of the user wants to specialise my_func for his type, which is an alias to std type. Surely define it…
3
votes
2 answers

How can I prevent overshadowing during ADL?

Suppose I have a class that extends a (STL) container and provides a customary begin member function: #include template struct Bar { Cont c; auto my_begin() { return begin(c); } }; int main() { …
Jodocus
  • 7,493
  • 1
  • 29
  • 45