我剛開始學習模板編程。我知道可以用兩種方式實例化一個函數模板,這兩種方法都是expilicit和implicit。考慮下面的代碼。零參數函數模板顯式實例化似乎不起作用
template <typename var>
void cool(){
var y = 45;
int i = 2;
}
template void cool<int>(); // instantiated here
int main(){
cool(); // error no matching function call. why?
cool<int>(); // works. whats the difference between both?
return 0;
}
當我使用的無參數的函數模板,我得到error: no matching function call for cool()
甚至當我明確實例化。但這種情況並非如此,當我使用的參數如下圖所示
template <typename var>
void cool(var x){
var y = 45;
int i = 2;
}
template void cool<int>(int); // instantiated here
int main(){
cool(24); // works
return 0;
}
我明白了明確instantation工作,只有當函數的參數有模板類型。如果函數的參數列表中沒有模板類型,它將被忽略。是對的嗎?或者有什麼我錯過了。任何幫助表示讚賞。