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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:42:15+00:00 2026-05-10T23:42:15+00:00

Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T

  • 0

Consider the following code:

#include <stdio.h>  namespace Foo {   template <typename T>   void foo(T *, int) { puts('T'); }    template <typename T>   struct foo_fun {     static void fun() { foo((T *)0, 0); };   }; }  namespace Foo {   void foo(int *, int) { puts('int'); } }  using namespace Foo;  int main() {   foo_fun<int> fun;   fun.fun(); } 

What’s the expected output? ‘T’ or int?

One compiler (gcc 4.0.1 from Apple’s Xcode 3.1.2) output ‘int’, two other compilers (gcc 4.1.2 and 4.1.3) output ‘T’.

If I move foo(int *, int) declaration/definition before the foo(T *, int) version, all output ‘int’. Is the order of overloading/specialization in this case defined by the current standard?

  • 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. 2026-05-10T23:42:15+00:00Added an answer on May 10, 2026 at 11:42 pm

    The second void foo(... is an overload (and not a specialization) which is not visible at the definition of foo_fun::fun so it won’t be found in the context of the template definition. Because T* is a dependent type, resolution of foo in the expression foo((T*)0, 0) will be delayed until template instantiation time and the context of the instantiation will also be considered. However, 14.6.4.2 of the standard says that if the function name is an unqualified-id but not a template-id then for non-ADL lookup only functions visible at the point of definition of the template are considered. There are no function arguments from the Foo namespace so no argument dependent lookup occurs, hence the template version of foo is called and not the non-template overload.

    Many thanks to litb for the corrections to this answer.

    If you made it a specialization as below, then as specializations are chosen at template instantiation time, the specialization can be called so long as the relevant specialization is visible at the point at which the function template is first instantiated for int.

    namespace Foo {     template<>     void foo<int>(int *, int) { puts('int'); } } 

    Chapter 14 of the current standard, but it’s not very readable 🙂

    Edit: If I had to pick the most relevant part of the standard it would probably be 14.6 [temp.res] para 9. (Slightly abbreviated) If a name does not depend on a template-parameter, a declaration for that name shall be in scope at the point at where the name appears in the template definition; the name is bound to the declaration found at that point and this binding is not affected by declarations that are visible at the point of instantiation.

    Edit, edit: But you also need to take into account 14.6.4.2 [temp.dep.candidate]. It is very difficult and dangerous to try and reference the standard because of all the interdependencies, this answer is a case in point.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I agree, too. No matter if it is C# or… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer You could handle some of it with your SQL query… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer The problem is that you have placed the content div… May 11, 2026 at 11:35 pm

Related Questions

Consider the following: volatile uint32_t i; How do I know if gcc did or
Consider the following code // BOGP.cpp : Defines the entry point for the console
Several comments on a recent answer of mine, What other useful casts can be
In an answer to Is it safe to store objects of a class which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.