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
14
votes
3 answers

Different behavior for qualified and unqualified name lookup for template

How should this code behave? It calls generic function ignoring my overload if I use qualified name in call_read() function; and it calls overload first and then generic version if I use unqualified name. What's the difference? Is it a bug in…
axe
  • 2,331
  • 4
  • 31
  • 53
14
votes
2 answers

Ambiguity between function and function in namespace with same argument

Can anybody explain why there is an ambiguity between A::f(const B& b) and f(const A::B& b). I consider the code to be quite explicit about the intention. #include namespace A { class B { protected: double value_; public: …
14
votes
1 answer

Comparison operator for std::vector fails to find comparison operator for T

The following very simple code won't compile #include #include namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ …
13
votes
2 answers

Non pre-declared function call works for class types but not primitive types

In the following code template void foo(T) { bar(T{}); } class Something {}; void bar(Something) {} int main() { foo(Something{}); } (https://wandbox.org/permlink/l2hxdZofLjZUoH4q) When we call foo() with a Something…
Curious
  • 20,870
  • 8
  • 61
  • 146
13
votes
2 answers

Why does endl(std::cout) compile

Surprisingly the below code compiles and runs without error on a variety of compilers and versions. #include int main() { endl(std::cout); return 0; } Ideone link How does it compile? I am sure there is no endl in global scope…
Gyapti Jain
  • 4,056
  • 20
  • 40
13
votes
1 answer

Which function is used to initialize the static class member?

I have a question about which function is chosen to init a static class member. //Base.h class Base { private: static int count; static int countInit() { return 10; } public: Base() { } }; //and Base.cpp static…
Donglei
  • 275
  • 1
  • 10
12
votes
1 answer

How does this template type deduction and overload resolution work?

Code #1 #include #include #include #include template void sort(typename container::iterator beginning, typename container::iterator end) { std::cout << "calling custom…
12
votes
2 answers

Apply using statement to the return type of a function without applying to the entire namespace

I'm trying to create a function that takes an underlying container, and returns a boost::iterator_range based on a custom iterator that does some processing on the elements. E.g. // The range class, templated on the underlying iterator…
happydave
  • 7,127
  • 1
  • 26
  • 25
12
votes
2 answers

What is C++ name lookup doing here? (& is GCC right?)

I was having a problem in some production code that I minimized to the following test case: template void intermediate(T t) { func(t); // line 4 ("func not declared in this scope") } namespace ns { struct type {}; } void…
EvanED
  • 947
  • 6
  • 22
12
votes
1 answer

Template overload resolution for operators inside an anonymous namespace

Short question: do operators have special template lookup rules for overload resolution with internal linkage or is the code at the bottom a template overload resolution bug for operators in GCC? The detail: instead of pasting a chunk of code I'll…
Thibaut
  • 2,400
  • 1
  • 16
  • 28
11
votes
2 answers

Beginning generically, plus decltype considering local using-declaration

C++0x's ranged-for loop has a special exception to handle arrays (FDIS §6.5.4), and there are two functions, std::begin and end, which are overloaded to handle arrays or to select begin/end methods. This leads me to believe a function accepting a…
Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
11
votes
2 answers

Hidden friends: declarations and definitions

In his recent blog post Anthony Williams talks about hidden friends. The main idea, if I understood it correctly, is that functions declared as friends cannot be found by ADL in certain situations. Simple example: namespace N { struct A { …
Evg
  • 25,259
  • 5
  • 41
  • 83
11
votes
2 answers

Why does the compiler choose the incorrect function overload in this case?

I'm trying out the code presented by Sean Parent at his talk at GoingNative 2013 - "Inheritance is the base class of evil". (code from the last slide available at https://gist.github.com/berkus/7041546 I've tried to achieve the same goal on my own…
10
votes
2 answers

Why does this program swap the values?

I have the following code: #include "stdafx.h" #include using namespace std; #include #include #include void swap(long a, long b) { long temp; temp=a; a=b; b=temp; } int _tmain(int argc,…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
10
votes
2 answers

GCC and ADL for operators in expressions

Consider this code sample template struct S { T t; }; template void foo(const S &v) { bar(v.t); } namespace N { struct A {}; } void bar(const N::A &a) {} int main() { S a; foo(a); } The code fails to…
1 2
3
24 25