读书人

函数模版特化有关问题

发布时间: 2012-02-13 17:20:26 作者: rapoo

函数模版特化问题
我的代码:
#include <iostream>
using namespace std;
template <class _Type>
class yyy
{
public:
template <class _T>
_T Min(_T first, _T second)
{
return first + second;
}
/*template <>
const char* Min(const char* first, const char* second)
{
char * c = "you are so pig ";
return c;
}*/
template <>
const char* Min(const char* first, const char* second);
};
template <class _Type> template <>
const char* yyy <_Type> ::Min(const char* first, const char* second)
{
char * c = "you are so pig ";
return c;
}
int main()
{
yyy <int> y;
cout < < y.Min(1,2) < < endl;
cout < < y.Min <const char *> ( "yy ", "uu ") < < endl;
system( "pause ");
}
我想在类模板外定义这个函数,可是始终不通过编译!
注释的是我在类内定义这个函数,编译通过,也运行很好!!看了好半天primer, 没有关于类外定义成员模板函数的语法, 望各位大虾指点,是否有语法的问题?

[解决办法]
没原因,规定而已,因为重载可以完成同样的功能,而编译器将好做很多。VC喜欢自己做扩展。

读书人网 >C++

热点推荐