关于这个问题,它确实具有相同的标题,但我在标准中找到了答案。我继续挖掘这个主题,最后找到了一个不适用这个答案的示例代码。
让我们考虑这段代码:
template<class T> void func(T* buf); //template I
template<size_t N> void func(char (&buf) [N]); //template II
void g(char (&buf)[3])
{
func(buf) //Error: ambiguous function call (Clang, GCC, ICC, MSVC)
}
根据[temp.func.order]和[temp.deduct.partial]中的偏序规则,如果通过执行这段代码来解释此规则,则模板 II将比模板 I更专业:
template <class T> void func1(T* buf) {}
template <std::size_t N> void func2(char (&buf)[N]) {}
struct invented_T{};
constexpr std::size_t invented_N=42;
void is_template_I_more_specialized(invented_T* buf)
{
func2(buf);
//DO NOT COMPILE
// => template I is not more specialized than func2
}
void is_template_II_more_specialized(char (&buf)[invented_N])
{
func1(buf);
//DO COMPILE
// => template II is more specialized than func1
}
所以按照这个解释,模板二应该更专业。为什么不是这样?