我知道下面的代码是一个类的部分特化:
template <typename T1, typename T2>
class MyClass {
…
};
// partial specialization: both template parameters have same type
template <typename T>
class MyClass<T,T> {
…
};
我也知道 C++ 不允许函数模板部分特化(只允许完整)。但是我的代码是否意味着我已经为一个/相同类型的参数部分专门化了我的函数模板?因为它适用于 Microsoft Visual Studio 2010 Express!如果不是,那么您能否解释一下部分专业化的概念?
#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;
}
原文由 Narek 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据标准,功能部分特化是不允许的。在示例中,您实际上是在 重载而不是专门 化
max<T1,T2>
函数。如果允许的话,它的 语法 应该 看起来 像下面这样:
在函数模板的情况下,C++ 标准只允许 完全 特 化——不包括编译器扩展!