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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:17:00+00:00 2026-06-04T19:17:00+00:00

I am trying to run some sample code from the book Accelerated C++ (A.

  • 0

I am trying to run some sample code from the book “Accelerated C++” (A. Koenig, B. Moo) (§8.2.2):

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <class In, class X>
In find(In begin, In end, const X &x)
{
    while (begin != end && *begin != x) {
        ++begin;
    }

    return begin;
}

int main()
{
    vector<int> v;

    v.push_back(5);
    v.push_back(32);
    v.push_back(42);
    v.push_back(7);

    cout << *find(v.begin(), v.end(), 42) << endl;

    return 0;
}

The find function appears like this in the book; the main function I wrote myself.

Both clang++ and g++ won’t compile this. It seems as if they are complaining that my find function introduced an ambiguity with std::find. However, I never used using namespace::std; nor using std::find; in the code, so the compiler shouldn’t even be allowed to use std::find if it was included. What’s going on here?

  • 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-04T19:17:02+00:00Added an answer on June 4, 2026 at 7:17 pm

    I think you’ve tripped over “Koenig lookup” (yes, the same Koenig, so you’d think he’d spot the problem), aka “ADL”.

    Suppose for a moment that via an indirect include, <algorithm> has been pulled in.

    If std::vector<int>::iterator (the type of the argument) is a class in namespace std, then std::find is a match for your call even though you never “used” it, so the call is ambiguous.

    If std::vector<int>::iterator is int*, then std::find is not a candidate and the call is not ambiguous.

    Either way is a valid implementation of std::vector, and it’s also implementation-defined whether or not either <iostream> or <vector> includes <algorithm>. So your code isn’t portable, but implementations are pretty much incapable of diagnosing the portability issue unless the code actually fails on that implementation.

    In more typical cases of ADL, the function or function template in the associated namespace is a better candidate (more specific) than the one in the namespace that the caller inhabits or “uses”, so ambiguity is avoided. But your global find is basically identical to std::find, so that doesn’t apply.

    http://ideone.com/Cskur:

    #include <iostream>
    
    namespace foo {
        struct Foo {};
    
        /* same as the one we'll define later */
        template <typename T>
        void func(T) {
            std::cout << "namespace function template\n";
        }
        /* this would be a better match
        void func(Foo) {
            std::cout << "namespace function\n";
        }
        */
    }
    
    template <typename T>
    void func(T) {
        std::cout << "global function template\n";
    }
    
    int main() {
        foo::Foo f;
        func(f);
    }
    

    Result:

    prog.cpp:19: error: call of overloaded ‘func(foo::Foo&)’ is ambiguous
    

    Conclusion: it is somewhat dangerous to use names from std, even in other namespaces.

    Or if you prefer: defining names from std in other namespaces may mean that callers need to be careful. The difference is mostly a matter of who fixes the bug when it happens.

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

Sidebar

Related Questions

I am trying to run some of the sample example code from CGAL as
trying to run the sample code in the Apress book called Beginning Scala. I
I'm trying to execute some sample code from the Thrust Quick Start Guide. It's
Ultimately I am trying to run some code which depends on the value (or
I am trying to run some Scala code on my OSX machine and keep
I'm a student, and I'm trying to write and run some test code for
I'm trying to run a .exe that requires some parameters by using system(). If
I was trying to run a sample code While launching the application in the
So I was trying to run some simple code with Code Contracts (that I
Some sample, straight forward gtk2 within-main-loop I'm currently trying to add code at key-press-event

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.