问题
In reference to this question, which has indeed the same title but for which I found an answer in the standard. I have continued to dig the subject and finaly find out an example code for which this answer does not apply.
Let's consider this piece of code:
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)
}
According to the partial ordering rules in [temp.func.order] and [temp.deduct.partial], template II shall be more specialized than template I if one interpreted this rules through the execution of this piece of code:
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
}
So according to this interpretation, template II should be more specialized. Why would it not be the case?
回答1:
As n.m. pointed out in the comment, the reason is that type T*
cannot be deduced from type char (&buf)[invented_N]
.
In is_template_II_more_specialized
, an extra array-to-pointer conversion is applied according to [temp.deduct.call]/2.1:
If P is not a reference type:
If A is an array type, the pointer type produced by the array-to-pointer standard conversion is used in place of A for type deduction; otherwise,
...
This rule only applies for deducing template arguments from a function call. For deducing template arguments during partial ordering, there is no such conversion applying.
Conversions that can be applied during partial ordering are described in [temp.deduct.partial]/5,6,7.
来源:https://stackoverflow.com/questions/48429659/why-c-template-accepting-array-is-not-more-specialized-than-one-accepting-poin