函式樣板的明確特製化
透過函式樣板,您可以提供該類型的明確特製化 (覆寫) 函式樣板,來定義該特定類型的特殊行為。 例如:
template<> void MySwap(double a, double b);
此宣告可讓您定義 double
變數的不同函式。 如同非樣板函式,會套用標準類型轉換(例如將 類型的 float
變數升階為 double
)。
範例
// explicit_specialization.cpp
template<class T> void f(T t)
{
};
// Explicit specialization of f with 'char' with the
// template argument explicitly specified:
//
template<> void f<char>(char c)
{
}
// Explicit specialization of f with 'double' with the
// template argument deduced:
//
template<> void f(double d)
{
}
int main()
{
}
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應