问题:
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;
}