是吗
template <typename T>
void foo(T) {}
template <>
void foo(int) {}
显式专用化或函数重载,而对于显式初始化编译器想看下面的代码吗?
template <typename T>
void foo(T) {}
template <>
void foo<int>(int) {}
我认为该标准接受以下两种情况:
ISO/IEC 14882:2011
14.7.3 Explicit specialization [temp.expl.spec] 1 ... can be declared by a declaration introduced by template<>; that is: explicit-specialization: template < > declaration
两者都是允许的。 在专业化中
template <>
void foo(int) {}
编译器从函数参数推断模板参数T = int
。从 14.7.3p10:
尾随模板参数可以在命名显式函数模板专用化的模板 id 中保留为未指定,前提是可以从函数参数类型推断出尾随模板参数。
给出的示例涉及从类模板中扣除,但它同样适用于从直接使用的类型中扣除:
template<class T> class Array { /∗ ... ∗/ };
template<class T> void sort(Array<T>& v);
// explicit specialization for sort(Array<int>&)
// with deduced template-argument of type int
template<> void sort(Array<int>&);