Why the overload resolution for the call max(x, y)
in the expression return max(max(x, y), z);
below results in a call to the non-template function char const* max(char const*, char const*)
?
Khi gọi chức năng này:
template <typename T>
T const& max (T const& x, T const& y, T const& z)
{
return max (max(x, y), z);
}
T
được suy luận là const char*
. Do đó, chữ ký này được khởi tạo:
const char* const& max (
const char* const& x,
const char* const& y,
const char* const& z
)
Chức năng nội bộ gọi là phiên bản nhị phân của max()
với các tham số kiểu const char*
. Cả mẫu và quá tải mẫu không khả thi đối với một đối số kiểu const char*
.
Tuy nhiên, khi hai chức năng là khả thi để giải quyết các cuộc gọi và một trong số họ không phải là một mẫu, phiên bản không mẫu được coi là một tốt nhất phù hợp.
mỗi khoản 13.3.3/1 của C++ 11 Tiêu chuẩn:
Given these definitions,** a viable function F1 is defined to be a better function than another viable function F2 if** for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then
— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,
— the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. [ ... ] or, if not that,
— F1 is a non-template function and F2 is a function template specialization, or, if not that,
— F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.
Điều này giải thích lý do tại sao không mẫu quá tải được chọn.
@BoPersson Hai chức năng trong ví dụ này không có chữ ký giống nhau. – Belloc