假设我们需要一个函数模板,该模板应根据类型返回整数:
template<typename T>
int get_type();
此外,我们通过几种类型来专门化它:
template<>
int get_type<int>()
{
return TYPE_INT;
}
// And so on for other types...
而且这很好,但不适用于数组类型。我可以执行以下操作:
template<>
int get_type<char[]>()
{
return TYPE_STRING;
}
并且编译器对此表示“同意”,但是链接器不同意。因为类型char[]
与例如char[5]
不同。
有什么方法可以实现此功能模板without函数参数?也就是说,我知道我们可以做这样的事情:
template<typename T>
int get_type(const T&);
但是,实际上,这里不需要(使用)功能参数。
编辑:
我使用C ++ 11。
您不能部分专用于具有大小的数组的函数。但是您可以在课堂上做到这一点。
您需要部分专业化以解决可变数组长度,并且C ++不允许部分专业化功能模板。规范的解决方案是(部分)专门化具有静态成员(函数)的class template