C++ 函數模板偏特化? - C++ function template partial specialization?

問題:

I know that the below code is a partial specialization of a class:我知道下面的代碼是一個類的部分特化:

template <typename T1, typename T2> 
class MyClass { 
  … 
}; 


// partial specialization: both template parameters have same type 
template <typename T> 
class MyClass<T,T> { 
  … 
}; 

Also I know that C++ does not allow function template partial specialization (only full is allowed).我也知道 C++ 不允許函數模板部分特化(只允許完整)。 But does my code mean that I have partially specialized my function template for one/same type arguments?但是我的代碼是否意味着我已經部分地爲一個/相同類型的參數專門化了我的函數模板? Because it works for Microsoft Visual Studio 2010 Express!因爲它適用於 Microsoft Visual Studio 2010 Express! If no, then could you please explain the partial specialization concept?如果不是,那麼您能否解釋一下部分專業化的概念?

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

template <typename T1, typename T2> 
inline T1 max (T1 const& a, T2 const& b) 
{ 
    return a < b ? b : a; 
} 

template <typename T> 
inline T const& max (T const& a, T const& b)
{
    return 10;
}


int main ()
{
    cout << max(4,4.2) << endl;
    cout << max(5,5) << endl;
    int z;
    cin>>z;
}

解決方案:

參考: https://stackoom.com/en/question/Xp9U
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章