功能模板专业化类型 - 是可选的吗? [英] Function template specialization type - is it optional?
本文介绍了功能模板专业化类型 - 是可选的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码中< const char *>
是可选的吗?我发现g ++和clang编译没有它很好。
Is the <const char*>
optional in below code? I found that g++ and clang compiles without it just fine.
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
// ...
}
所以是的,在这种情况下是可选的。
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) {
// ...
}
这篇关于功能模板专业化类型 - 是可选的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文