Home > Backend Development > C++ > Why Does Pointer Decay Take Precedence Over Deduced Templates in C Overload Resolution?

Why Does Pointer Decay Take Precedence Over Deduced Templates in C Overload Resolution?

Mary-Kate Olsen
Release: 2024-11-29 19:15:15
Original
775 people have browsed it

Why Does Pointer Decay Take Precedence Over Deduced Templates in C   Overload Resolution?

Pointer Decay vs. Deduced Templates: Unravelling the Priority Puzzle

In the realm of C , the interplay between function overloads and template deduction can sometimes lead to unexpected results. A prime example arises when a function is overloaded to handle both arrays and raw pointers. Consider the following code:

template <size_t N>
void foo(const char (&amp;s)[N]) {
    std::cout << "array, size=" << N - 1 << std::endl;
}

void foo(const char *s) {
    std::cout << "raw, size=" << strlen(s) << std::endl;
}
Copy after login

Initially devised to print the length of an array, this function is extended to support non-arrays. However, this extension leads to a puzzling ambiguity:

foo("hello") // now prints raw, size=5
Copy after login

Why is the "raw" overload chosen over the intended "array" version, despite the latter matching the parameters more precisely? The answer lies in a subtle concept known as pointer decay.

Pointer decay is an implicit conversion from an array to its corresponding pointer. In this case, the array "hello" is silently converted to a const char * pointer to its first element. As a result, the overload that handles pointers takes precedence.

This behavior stems from the cost of conversions in C . Overloads are evaluated to minimize the cost of converting arguments to parameters. In this case, the array-to-pointer conversion is less costly than an array-to-function parameter conversion.

To work around this issue, one can define the second overload as a template as well:

template <typename T>
auto foo(T s)
-> std::enable_if_t<std::is_convertible<T, char const *>{}>
{
    std::cout << "raw, size=" << std::strlen(s) << std::endl;
}
Copy after login

This approach ensures that the template version is preferred because it eliminates the pointer decay conversion.

In conclusion, the priority given to pointer decay over deduced templates is a consequence of the cost-minimization principle in overload resolution. To avoid ambiguities, it is crucial to consider both implicit conversions and the types of overloads when overloading functions.

The above is the detailed content of Why Does Pointer Decay Take Precedence Over Deduced Templates in C Overload Resolution?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template