4

In my interpretation of these overload rules, the code sample below should use the template method, as void foo(const char (& a)[Size]) does not require any conversion when called by const char[13]. However, the compiler evidently prefer to convert a to const char*, which I believe would break this rule:

  1. there is at least one argument of F1 whose implicit conversion is better than the corresponding implicit conversion for that argument of F2

Am I thinking about conversions in the wrong way? Code sample in question:

#include <iostream>

template <size_t Size>
void foo(const char (& a)[Size])
{
    std::cout << a << " (array [" << Size << "])\n";
}
void foo(const char * a)
{
    std::cout << a << " (ptr)\n";
}

int main()
{
    const char a[] = "Hello Array!";

    foo(a); // Prints: "Hello Array! (ptr)"
}

0 Answers0