簡體   English   中英

使用相同名稱重載不同功能模板之間的優先級

[英]Overloading priority between different function templates with identical name

抱歉標題不清楚,如果找到更好的標題,請隨時編輯。 在正常功能和模板功能之間的優先級中已經深入討論了相關主題,但我沒有找到我的問題的答案。

我的代碼是:

template<typename T>
void f(T t){std::cout << "Template 1" << std::endl;} // template 1

template<typename T, typename B>
void f(T t){std::cout << "Template 2" << std::endl;} // template 2

int main () {
   f(1);  // line 1, template 1 will be called
   f<int>(1);  // template 1 will be called
   f<int,int>(1);  // template 2 will be called
}

在第1行調用模板1函數的可能原因是什么? 它在規范中是否定義明確?

在第1行,我認為編譯器應該給出“模糊過載”錯誤。

無法推導出B (沒有參數具有類型B ),因此模板1是唯一可能的超載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM