C ++ - The best viable function; matrix decay to pointer is not a transformation?

In my interpretation of these overload rules , the code sample below should use the template method, since it void foo(const char (& a)[Size])does not require any conversion when called const char[13]. However, the compiler obviously prefers to convert ato const char*, which, in my opinion, violates this rule:

  • there is at least one argument F1 whose implicit conversion is better than the corresponding implicit conversion for this argument F2

Am I thinking of conversions wrong? Code example:

#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)"
}
+4

Source: https://habr.com/ru/post/1685464/


All Articles