Two function template overloading principles?

for function overloading, it is generally determined according to the function formal parameter list, so it should be the same for function templates, but for the following code
when calling function compare ("ab", "ab") reports an error, it is right to call compare ("ab", "abc"). It is not clear why int compare (const T & v1, const T & v2) does not match, because the length of the string array is inferred. Think the second one is more accurate? Can the compiler also find this difference?

-sharpinclude "pch.h"
-sharpinclude <iostream>

template <typename T>
int compare( const T& v1, const T& v2 ) {
    if ( v1 < v2 ) return -1;
    if (v1 > v2) return 1;
    return 0;
}


template<unsigned N, unsigned M>
int compare(const char(&p)[N], const char(&p2)[M]) {
    std::cout << p << "  " << p2 << std::endl;
    return strcmp(p, p2);
}

int main()
{
    //int res2 = compare( "ab", "ab" );//
}
CPP
Jul.06,2021

because the first template, the two template parameter (function template parameters) are the same, so as long as the array you pass in is the same size, the first one is feasible, inferred is the array reference type, and the array reference type includes the array size. const char (& a) [3] is the same type as const char (& b) [3] , while const char (& a) [3] and const char (& b) [4] are two different types.

change the second function template to be similar to the first:

-sharpinclude <iostream>
using namespace std;

template<unsigned N>
int compare(const char(&p)[N], const char(&p2)[N]) {
    cout << "unsigned" << endl;
    return 0;
}


int main() {
    compare("ab", "acd");
}

the compilation error at this time is the same as the one in your question.

Menu