[英]Function template specialization type - is it optional?
以下代码中的<const char*>
可选? 我发现g ++和clang编译没有它就好了。
template<typename T>
void debugRep2(T const& t) {
std::cout << "debugRep(const T& t)\n";
}
template<>
void debugRep2<const char*>(const char* const& t) {
//^^^^^^^^^^^^^
std::cout << "const char*& t\n";
}
int main() {
int n;
int *pn = &n;
debugRep2(n);
debugRep2(pn);
}
模板化类型已在函数参数中指定,可由编译器推导出
template<>
void debugRep2<const char*>(const char* const& t) {
// ^^^^^^^^^^^ already present
// ...
}
所以是的,在这种情况下它是可选的。
实际上,编写该专业化的常用方法是
template<>
void debugRep2(const char* const& t) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.