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 3396480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:24:47+00:00 2026-05-18T04:24:47+00:00

Some time ago I read an article that explained several pitfalls of argument dependent

  • 0

Some time ago I read an article that explained several pitfalls of argument dependent lookup, but I cannot find it anymore. It was about gaining access to things that you should not have access to or something like that. So I thought I’d ask here: what are the pitfalls of ADL?

  • 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-05-18T04:24:47+00:00Added an answer on May 18, 2026 at 4:24 am

    There is a huge problem with argument-dependent lookup. Consider, for example, the following utility:

    #include <iostream>
    
    namespace utility
    {
        template <typename T>
        void print(T x)
        {
            std::cout << x << std::endl;
        }
    
        template <typename T>
        void print_n(T x, unsigned n)
        {
            for (unsigned i = 0; i < n; ++i)
                print(x);
        }
    }
    

    It’s simple enough, right? We can call print_n() and pass it any object and it will call print to print the object n times.

    Actually, it turns out that if we only look at this code, we have absolutely no idea what function will be called by print_n. It might be the print function template given here, but it might not be. Why? Argument-dependent lookup.

    As an example, let’s say you have written a class to represent a unicorn. For some reason, you’ve also defined a function named print (what a coincidence!) that just causes the program to crash by writing to a dereferenced null pointer (who knows why you did this; that’s not important):

    namespace my_stuff
    {
        struct unicorn { /* unicorn stuff goes here */ };
    
        std::ostream& operator<<(std::ostream& os, unicorn x) { return os; }
    
        // Don't ever call this!  It just crashes!  I don't know why I wrote it!
        void print(unicorn) { *(int*)0 = 42; }
    }
    

    Next, you write a little program that creates a unicorn and prints it four times:

    int main()
    {
        my_stuff::unicorn x;
        utility::print_n(x, 4);
    }
    

    You compile this program, run it, and… it crashes. “What?! No way,” you say: “I just called print_n, which calls the print function to print the unicorn four times!” Yes, that’s true, but it hasn’t called the print function you expected it to call. It’s called my_stuff::print.

    Why is my_stuff::print selected? During name lookup, the compiler sees that the argument to the call to print is of type unicorn, which is a class type that is declared in the namespace my_stuff.

    Because of argument-dependent lookup, the compiler includes this namespace in its search for candidate functions named print. It finds my_stuff::print, which is then selected as the best viable candidate during overload resolution: no conversion is required to call either of the candidate print functions and nontemplate functions are preferred to function templates, so the nontemplate function my_stuff::print is the best match.

    (If you don’t believe this, you can compile the code in this question as-is and see ADL in action.)

    Yes, argument-dependent lookup is an important feature of C++. It is essentially required to achieve the desired behavior of some language features like overloaded operators (consider the streams library). That said, it’s also very, very flawed and can lead to really ugly problems. There have been several proposals to fix argument-dependent lookup, but none of them have been accepted by the C++ standards committee.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago I put together a time based library that could be used
some time ago I found an article ( Roles: Composable Units of Object Behavior
Some time ago, I came across a piece of code, that used some piece
I learned some time ago about Decision Trees and Decision tables. I feel that
I remember coming across an article on I think CodeProject quite some time ago
Some time ago I got this error when building ANY Visual Studio Deployment project.
Some time ago I wrote a little piece of code to ask about on
Background: Some time ago, I built a system for recording and categorizing application crashes
I remember some rules from a time ago (pre-32bit Intel processors), when was quite
A good while ago, I read an article by the creator of viemu ,

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.