[英]Function template specialization type - is it optional?
Is the <const char*>
optional in below code? 以下代码中的
<const char*>
可选? I found that g++ and clang compiles without it just fine. 我发现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);
}
The templated type is already specified at the function parameter and can be deduced by the compiler 模板化类型已在函数参数中指定,可由编译器推导出
template<>
void debugRep2<const char*>(const char* const& t) {
// ^^^^^^^^^^^ already present
// ...
}
So yes, in this case it is optional. 所以是的,在这种情况下它是可选的。
In fact the common way to write that specialization would be 实际上,编写该专业化的常用方法是
template<>
void debugRep2(const char* const& t) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.