Is the <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) {
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)