【发布时间】:2014-01-10 03:16:50
【问题描述】:
我目前正在尝试为显示不同值的力量的函数编写模板特化。 我的代码是:
#include <iostream>
using namespace std;
template <class T>
T myfunc(T x)
{
return x*x;
}
template<>
string myfunc<string>(string ss)
{
return (ss+ss);
}
int main()
{
cout<<myfunc("test")<<endl;
return 0;
}
编译失败:
错误 C2784:'std::_String_iterator<_mystr> std::operator +(_String_iterator<_mystr>::difference_type,std::_String_iterator<_mystr>)' : 无法推断出模板参数 来自“std::string”的“std::_String_iterator<_mystr>”
您能帮我找出问题所在吗?
【问题讨论】:
-
"test"不会被推断为std::string。它的类型为const char[5]。