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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:45:09+00:00 2026-06-02T04:45:09+00:00

#include <iostream> void foo() { std::cout << global foo() << std::endl; } struct A

  • 0
#include <iostream>

void foo()
{
    std::cout << "global foo()" << std::endl;
}

struct A {
    void foo()
    {
        std::cout << "A::foo()" << std::endl;
    }
};

struct B : public A {
    void call()
    {
        foo();
    }
};

int main(int argc, char **argv )
{
    B b;
    b.call();
    return 0;
}

This gives expected result:

A::foo()

However after changing two lines (class B to template):

#include <iostream>

void foo()
{
    std::cout << "global foo()" << std::endl;
}

struct A {
    void foo()
    {
        std::cout << "A::foo()" << std::endl;
    }
};

template <typename T> // change here
struct B : public T {
    void call()
    {
        foo();
    }
};

int main(int argc, char **argv )
{
    B<A> b; // and here
    b.call();
    return 0;
}

I get unexpected result:

global foo()

And using this-> is not an option as I am trying to create a “fallback” mechanism.

  • 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-02T04:45:11+00:00Added an answer on June 2, 2026 at 4:45 am

    What you get is an expected result. This is called “Two-phase name lookup” in the C++ standard.

    Names inside templates are divided into two types:

    Dependent – names that depend on the template parameters but aren’t declared within the template.

    Non-dependent – names that don’t depend on the template parameters, plus the name of the template itself and names declared within it.

    When the compiler tries to resolve some name in the code, it first decides whether the name is dependent or not, and the resolution process stems from this distinction. While non-dependent names are resolved “normally” – when the template is defined, the resolution for dependent names happens at the point of the template’s instantiation.

    foo(); in B::call in your example is a non-dependent name, so it is resolved to global foo() at the point of template definition.

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

Sidebar

Related Questions

#include <iostream> namespace { int a=1; } int a=2,b=3; int main(void) { std::cout<<::a<<::b; return
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include <iostream> template <class T> void foo(T) { std::cout << "foo(T)" << std::endl; }
#include <iostream> class A { public: void foo() const { std::cout << const version
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
#include <iostream> using namespace std; int main(void){ int size = -2; int* p =
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
Consider next example : #include <iostream> template< int a > void foo(); int main(int

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.