Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:26:21+00:00 2026-06-16T21:26:21+00:00

I’m having trouble understanding the rules behind argument-dependent (Koenig) lookup. Consider the code below:

  • 0

I’m having trouble understanding the rules behind argument-dependent (Koenig) lookup.

Consider the code below:

#include <iostream>

using namespace std;

namespace adl
{
    struct Test { };
    void foo1(Test const &) { cout << "ADL used (foo1)" << endl; }
    void foo2(Test const &) { cout << "ADL used (foo2)" << endl; }
    void foo3(Test const &) { cout << "ADL used (foo3)" << endl; }
}

struct foo1
{
    foo1() { }

    template<class T>
    foo1(T const &) { cout << "ADL not used (foo1)" << endl; }

    template<class T>
    void operator()(T const &) const { cout << "ADL not used (foo3)" << endl; }
};

template<class T> void foo2(T const &)
{ cout << "ADL not used (foo2)" << endl; }

int main()
{
    adl::Test t;
    foo1 foo3;
    (foo1(t));
    (foo2(t));
    (foo3(t));
}

Its output is:

ADL not used (foo1)
ADL used (foo2)
ADL not used (foo3)

I expected all of them to use ADL, but I was surprised that only some of them did.

What are the (potentially gory, I know) details behind the rules of ADL?
I understand the concept well enough, but the details are what I’m having trouble with.

Which scopes are searched, when are they searched, and when are they not searched?

Is it at all possible to tell whether ADL is used without having to look through all the #include‘d files before the given line of code? I expected functors and functions to behave the same way in terms of [not] masking ADL, but apparently they don’t.

Is there any way to force ADL in cases where it is not done automatically (such as the above) and you don’t know the class’s namespace (e.g. in a template)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:26:23+00:00Added an answer on June 16, 2026 at 9:26 pm

    Your problem isn’t really with argument dependent lookup. First of all, argument dependent lookup only possibly enters the picture when doing unqualified looking up of functions. When calling foo1(t) foo1 is a type and its templated constructor is called. Similarly, foo3(t) is a qualified lookup because foo3 is an object and the function call operator is looked up in the object’s class foo1. The only place where argument lookup enters the picture is calling foo2(t) where lookup finds to candidates:

    1. ::foo2<adl::Test>(adl::Test const&)
    2. ::adl::foo2(adl::Test const&)

    These two functions are handed off to overload resolution and since both functions are equally good matches the non-template function wins.

    Your question are actually three questions:

    1. The gory details of name lookup are too broad and, thus, this question is a request for an essay to be written which I ignore.
    2. You second question expands to three more questions, only one seems relevant:
      1. Which scopes are searched? When looking up an unqualified function name inside a function definition the rules depend on whether any of the names is a dependent name. If there is no such name (i.e., in non-template code or in template code where the names can be determined in phase one), the name is looked up enclosing namespaces and in namespaces associated with its argument. Otherwise, the name is only looked up in the associated namespaces.
    3. Can argument dependent lookup be forced? It is always done for unqualified function lookups if there is, at least, one argument but names found otherwise may be better matches. Of course, you need to call an unqualified function otherwise it won’t be done.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.