0

Consider the following code, taken from https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/name_binding.htm:

#include <iostream>
using namespace std;

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

template <class A> struct container{ // point of definition of container
   void member1(){
      // This call is not template dependent, 
      // because it does not make any use of a template parameter.
      // The name is resolved at the point of definition, so f(int) is not visible.
      f(1); 
   }
   void member2(A arg);
};

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

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

template <class A> void container<A>::member2(A arg){ 
   // This call is template dependent, so qualified name lookup only finds
   // names visible at the point of instantiation.
   ::h(arg);  
}

template struct container<int>; // point of instantiation of container<int>

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

int main(void){   
   container<int> test;   
   test.member1();
   test.member2(10);
   return 0;
}

The output is

Function f(double)
Function h(double)

I understand this, but what I don't understand, when the article states

The point of instantiation of a template is located immediately before the declaration that encloses its use. In this example, the point of instantiation of container is the location of the explicit instantiation

...is why when I move the definition of void h(int) above what is labelled as the point of instantiation, h(int) still does not get called. It only gets called when I move it above the definition of the function void container<A>::member2(A).

This is the case in VS2017 and g++, so clearly either the article is badly worded or I am missing something. Can someone please clarify?

Wad
  • 1,454
  • 1
  • 16
  • 33
  • Possible duplicate of https://stackoverflow.com/questions/30514337/point-of-instantiation-and-name-binding – P.W Feb 21 '19 at 12:40
  • Possible duplicate of [point of instantiation and name binding](https://stackoverflow.com/questions/30514337/point-of-instantiation-and-name-binding) – Alan Birtles Feb 21 '19 at 13:03

1 Answers1

0

OK, the link posted above (point of instantiation and name binding) ultimately does answer this question, however it is not as clear as it could be. I have found this page and this page which further aided in my understanding, hence I post them here so others can benefit.

Wad
  • 1,454
  • 1
  • 16
  • 33