您的位置:首页 > 其它

今天使用模板特化所遇到的问题

2007-01-01 21:02 204 查看
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;
}

上述代码是可以在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;
}

但是,如果写成这样:

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;
}

就会出现问题:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: