【发布时间】:2011-07-24 18:52:37
【问题描述】:
我正在使用/学习模板函数专业化规则。我从这个函数开始
template<typename T>
std::string toString(const T& t)
{
ostringstream out;
out << t;
return out.str();
}
现在我想将它专门用于 const char*
typedef const char* ccharPtr;
template<>
std::string toString(const ccharPtr& s)
{
cout << "in specialization" << endl; // just to let me know
return std::string(s);
}
我想在没有 typedef 的情况下这样做,但到目前为止我无法弄清楚。
该特化适用于 const char*,但不适用于 char*。
const char* s1 = "Hi"
cout << toString(s1); // works
char s2[] = "There";
cout << toString(s2); // doesn't work, since s2 isn't const char*
cout << toString(", Bob"); // doesn't work. Why not?
我想要一个专长来处理每个案例,但很难搞清楚。
【问题讨论】: