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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:07:18+00:00 2026-06-11T18:07:18+00:00

Consider this piece of code: #include <iostream> #include <vector> template<typename A> void foo(A& a)

  • 0

Consider this piece of code:

#include <iostream>
#include <vector>

template<typename A>
void foo(A& a) {
    std::cout << "the wrong foo" << std::endl;
}

template<typename A>
void do_stuff(A& a) {
    foo(a);
}

template<typename X>
void foo(std::vector<X>& a) {
    std::cout << "the right foo" << std::endl;
}

int main()
{
    std::vector<int> q;
    do_stuff(q);
}

Why is it calling the “wrong” foo? If the first declaration of foo is removed the right foo is called.

I am using gcc 4.6.3.

Update:
If functions are declared in the following order, the right foo is called.

template<typename A> void do_stuff(A& a) { ... }
template<typename A> void foo(A& a) { ... }
template<typename X> void foo(std::vector<X>& a) { ... }
  • 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-11T18:07:19+00:00Added an answer on June 11, 2026 at 6:07 pm

    The observed behavior is correct, as foo(a) is a type dependent expression according to:

    14.6.2.2 Type-dependent expressions                         [temp.dep.expr]
    
    1) Except as described below, an expression is type-dependent if any
       subexpression is type-dependent.
    
    2) this is type-dependent if the class type of the enclosing member
       function is dependent (14.6.2.1).
    
    3) An id-expression is type-dependent if it contains
    
        — an identifier associated by name lookup with one or more declarations 
          declared with a dependent type,
        ...
    

    and under 14.6.4 (Dependent name resoultion):

    14.6.4.2 Candidate functions                              [temp.dep.candidate]
    
    For a function call that depends on a template parameter, the candidate
    functions are found using the usual lookup rules (3.4.1, 3.4.2, 3.4.3) except
    that:
    
    — For the part of the lookup using unqualified name lookup (3.4.1) or qualified
      name lookup (3.4.3), only function declarations from the template definition 
      context are found.
    — For the part of the lookup using associated namespaces (3.4.2), only function
      declarations found in either the template definition context or the template
      instantiation context are found.
    
    If the function name is an unqualified-id and the call would be ill-formed or
    would find a better match had the lookup within the associated namespaces
    considered all the function declarations with external linkage introduced in
    those namespaces in all translation units, not just considering those
    declarations found in the template definition and template instantiation
    contexts, then the program has undefined behavior.
    

    The “wrong” foo() is picked because that’s the only one visible at the point of template definition, and the “right” foo() is not considered because it’s not in a namespace associated with the types of the function arguments.

    If you modify your code so that the “right” foo() would be in an associated namespace, it would be picked instead of the “wrong” foo(). (In this particular case, it’s not allowed by the standard, so don’t do the below, but with your own namespace / types this is how it should work)

    #include <iostream>
    #include <vector>
    
    template<typename A> void foo(A& a)
    {
        std::cout << "the wrong foo" << std::endl;
    }
    
    template<typename A>
    void do_stuff(A& a) {
        foo(a);
    }
    
    namespace std { // evil, don't do this with namespace std!
    
    template<typename X>
    void foo(std::vector<X>& a) {
        std::cout << "the right foo" << std::endl;
    }
    
    }
    
    int main()
    {
        std::vector<int> q;
        do_stuff(q); // calls the "right" foo()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
Consider this piece of code var crazy = function() { console.log(this); console.log(this.isCrazy); // wrong.
Consider this piece of code: class A { void methodX() { // snip (1
Consider this piece of code: int *a, *b; a = foo(); if (a) b
Consider this one piece of Perl code, $array[$x]->{foo}->[0]= January; I analyze this code as
consider this piece of scala swing code detail.reactions += { case ButtonClicked(but) => detail.contents
consider this baby simple piece of code pg.connect( { user: 'hhope', password: '...', port:
Consider this piece of code: public abstract class Validator { protected Validator() { }

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.