[英]Point of instantiation clarification
考慮以下代碼,該代碼取自https://www.ibm.com/support/knowledgecenter/zh-CN/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;
}
輸出是
Function f(double)
Function h(double)
我理解這一點,但是當文章指出時我不了解
模板的實例化點位於緊隨其使用的聲明之前。 在此示例中,容器的實例化點是顯式實例化的位置
......就是為什么當我移動的定義void h(int)
上面什么標記為實例化點, h(int)
仍不會被調用。 僅當我將其移到函數 void container<A>::member2(A)
的定義上方時,它才會被調用。
在VS2017和g ++中就是這種情況,因此很明顯,要么文章措辭不當,要么我遺漏了一些東西。 有人可以澄清一下嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.