【发布时间】:2010-09-16 04:05:54
【问题描述】:
这是由this 文章(第 5 页)激发的
template<class T>
T const &f(T const &a, T const &b){
return (a > b ? a : b);
}
template int const &f<int>(int const &, int const &);
int main(){
int x = 0, y = 0;
short s = 0;
f(x, y); // OK
f(x, s); // Is this call well-formed?
}
电话'f(x, s)' 格式正确吗?我假设由于函数模板'f' 是显式实例化的,因此将应用标准转换,因此'short s' 将转换为'int' 以匹配对显式特化'f<int>' 的调用。但这似乎是不正确的?
标准的哪一部分谈到了在这种情况下的适用规则?
【问题讨论】:
-
如果我了解您的出处,您可能想在drdobbs.com/cpp/184403774 阅读有关 Alexandrescu 的最小/最大实现的信息 - 古老但有趣。 (我还没有重读它,看看它是如何老化的)
-
专门化一个函数模板通常是个坏主意。请参阅 Herb Sutter 的 "Why not Specialize Function Templates?" 了解替代方案。
标签: c++ templates explicit-specialization