template<class T>
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test<int>(const int& x)
{
cout<<"void test(const int& x)"<<endl;
}
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test<int>(const int& x)
{
cout<<"void test(const int& x)"<<endl;
}
上述代码是可以在VS2005上编译通过的,另外一种写法是:
template<class T>
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test(const int& x)//此处没有<int>
{
cout<<"void test(const int& x)"<<endl;
}
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test(const int& x)//此处没有<int>
{
cout<<"void test(const int& x)"<<endl;
}
但是,如果写成这样:
template<class T>
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test<const int&>(const int& x)
{
cout<<"void test(const int& x)"<<endl;
}
void test(const T& t)
{
cout<<"void test(const T& t)"<<endl;
}
template<>
void test<const int&>(const int& x)
{
cout<<"void test(const int& x)"<<endl;
}
就会出现问题:
d:codejob est est est.cpp(19) : error C2770: invalid explicit template argument(s) for 'void test(const T &)'
d:codejob est est est.cpp(10) : see declaration of 'test'
d:codejob est est est.cpp(19) : error C2912: explicit specialization; 'void test<const int&>(const int &)' is not a specialization of a function template
d:codejob est est est.cpp(10) : see declaration of 'test'
d:codejob est est est.cpp(19) : error C2912: explicit specialization; 'void test<const int&>(const int &)' is not a specialization of a function template