您的位置:首页 > 编程语言 > C语言/C++

C++ 函数模板的特化(Function Template Specialization)

2015-09-23 11:46 579 查看
C++ 函数模板的特化(Function Template Specialization)

flyfish 2015-9-23

某些情况下,通用模板定义对于某个类型可能是完全错误的,通用模板定义也许不能编译或者做错误的事情。

In some case ,the general template definition is simply wrong for a type:The general definition might not compile or might do the wrong thing。

非常好的例子

//first version  can compare any two types
template <typename T>
int compare(const T &v1,const T &v2)
{
if(v1 < v2)return -1;
if(v2 < v1)return 1;

return 0;
}

//special version of compare to handle C-style character strings
template<>
int compare<const char *>(const char *const &v1,const char *const &v2)
{
return strcmp(v1,v2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: