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

  • Home
  • SEARCH
  • 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 6556007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:53:22+00:00 2026-05-25T12:53:22+00:00

Consider this example I found on IBM’s website: #include <iostream> using namespace std; void

  • 0

Consider this example I found on IBM’s website:

#include <iostream>
using namespace std;

void f(double) { cout << "Function f(double)" << endl; }

template<class T> void g(T a) {
  f(123);
  h(a);
}

void f(int) { cout << "Function f(int)" << endl; }
void h(double) { cout << "Function h(double)" << endl; }

void i() {
  extern void h(int);
  g<int>(234);
}

void h(int) { cout << "Function h(int)" << endl; }

int main(void) {
    i();
}

What will it print?

  • The IBM documentation I adapted this example from, available here, says it will print:

    Function f(double)
    Function h(double)
    

    The rationale for this is that template-parameter-dependent name lookup is performed right before the instantiation of i(), so it finds h(double) but not h(int).

  • When I compile it using GCC 4.4.1, it prints:

    Function f(double)
    Function h(int)
    

    GCC seems to be looking up the template-parameter-dependent names in the template after everything else has been compiled, so it finds both h(double) and h(int), and prefers the latter.

  • When I compile it using Clang 2.8, it fails to compile. The compiler error is:

    ibm_example.cc:8:3: error: use of undeclared identifier 'h'
      h(a);
      ^
    ibm_example.cc:16:3: note: in instantiation of function template specialization 'g<int>' requested here
      g<int>(234);
      ^
    1 error generated.
    

    Clang seems to be looking up the template-parameter-dependent names in the template at the point where the template is declared, so it finds neither h(double) nor h(int).

Which one is right?

  • 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-25T12:53:23+00:00Added an answer on May 25, 2026 at 12:53 pm

    They are all correct. No really, read on…

    template<class T> void g(T a) {
      f(123);
      h(a);
    }
    

    Here, f is a non-dependent name but h is a dependent name according to 14.6.2/1. f is looked up

    Non-dependent names used in a template definition are found using the usual name lookup and bound at
    the point they are used.

    f is looked up immediately and bound to void f(double), the only f visible at that point.

    According to 14.6.4.1 the point of instantiation of void g<int>(int) is immediately after the definition of void i(), where it is used.

    [..]Otherwise, the point of instantiation for such a specialization
    immediately follows the namespace scope declaration or definition that refers to the specialization.

    This means that sources for resolving the dependent name are declarations visible at the definition of template<class T> void g(T a) and “declarations from namespaces associated with the types of the function arguments both from the instantiation context (14.6.4.1) and from the definition context” (14.6.4).

    However, because int is a fundemantal type, the set of associated namespaces is empty (3.4.2) (no, not even the global namespace is included) and according to 14.6.4.2 it is only lookup using the associated namespaces that can use the template instantiation context, normal unqualified name lookup can only use what is visible at the template definition context. This confirms what was said in 14.6.4.

    Now, the bonus point. 14.6.4.2 goes on to say:

    If 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 call is ill formed because lookup fails (the part about associated namespaces doesn’t apply here), so the behavior is explicitly undefined so anything could happen. Hence none of the behaviours seen shows a non-conformance with the standard although, to me, Clang’s diagnostic seem most in keeping with the standard.

    (All references ISO/IEC 14882:2011.)

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

Sidebar

Related Questions

Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
Consider this classic example used to explain what not to do with forward declarations:
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this synthetic example. I have two native C++ projects in my Visual Studio

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.