且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于通过引用传递数组与传递指针的函数参数绑定规则

更新时间:2023-11-09 08:01:34

第一个重载与类型完全匹配,而第二个重载则需要额外的衰减到指针"步骤.

因为当检查过载分辨率,即 $ 16.3.3.1.1标准转换顺序[over.ics.scs]表13 —转换

 转化类别排名子条款无需转换身份完全匹配……数组到指针的转换左值变换精确匹配[转换数组]…… 

值得注意的是,无需任何转换"(即第一次过载的情况)的排名也是完全匹配".

To prevent any confusion, I very much understand the difference between arrays and pointers, the concept of decay-to-pointer, and the concept of passing an array by reference in C++, etc.

My question here is specifically about the rules used by the compiler to select a function from a set of function overload candidates, when one overload takes an array reference, and the other overload takes a pointer.

For example, suppose we have:

template <class T, std::size_t N>
void foo(const T (&arr)[N])
{
    std::cout << "Array-reference overload!" << std::endl;
}

template <class T>
void foo(const T* ptr)
{
    std::cout << "Pointer overload!" << std::endl;
}

If we attempt to invoke function template foo() as follows:

const char arr[2] = "A";
foo(arr);

... then my expectation would be that the first overload, the one that takes an array reference, would be selected by the compiler.

However, using GCC 4.9.2, if I compile the above code, I get an error:

test.cpp:28:9: error: call of overloaded ‘foo(const char [2])’ is ambiguous

It's unclear to me why both overloads are considered equally good candidates by the compiler here, since the first overload matches the type exactly, whereas the second overload requires an extra decay-to-pointer step.

Now, I am able to get the above overload working by explicitly using type_traits as follows:

template <class T, std::size_t N>
void foo(const T (&arr)[N])
{
    std::cout << "Array-reference overload!" << std::endl;
}

template <class T>
void foo(T ptr, typename std::enable_if<std::is_pointer<T>::value>::type* = 0)
{
    std::cout << "Pointer overload!" << std::endl;
}

In this case, the program compiles and the overload that takes an array reference is selected. However, I don't understand why this solution should be necessary. I'd like to understand why the compiler considers a function that requires decay-to-pointer an equally likely overload candidate as the array reference, when the argument passed is very much an array.

the first overload matches the type exactly, whereas the second overload requires an extra decay-to-pointer step.

Because when checking the ranking of implicit conversion sequences in overload resolution, the array-to-pointer conversion is considered as an exact match, thus the 2nd overload has the same rank with the 1st one.

From the standard, $16.3.3.1.1 Standard conversion sequences [over.ics.scs] Table 13 — Conversions

Conversion                   Category               Rank         Subclause
No conversions required      Identity               Exact Match
... ...
Array-to-pointer conversion  Lvalue Transformation  Exact Match  [conv.array]
... ...

It's worth noting that the rank of "No conversions required" (i.e. the case for the 1st overload) is "Exact Match" too.