while I came across this snippet in <<C++ Primer>>
template< unsigned N, unsigned M>
int compare(const char(&p1)[N], const char (&p2)[M]
{
return strcmp(p1, p2);
}
compare("hi", "mom")
sure it worked very well
then i thought what if if remove const
and &
?
so i wrote this
template< unsigned N, unsigned M>
int compare(char p1 [N], char p2 [M])
{
return strcmp(p1, p2);
}
compare("hi", "mom")
but i got this error
prog.cc: In function 'int main()':
prog.cc:34:12: error: no matching function for call to 'compare(const char [3], const char [4])'
34 | compare("hi", "mom");
| ~~~~~~~^~~~~~~~~~~~~
prog.cc:27:5: note: candidate: 'template<unsigned int N, unsigned int M> int compare(char*, char*)'
27 | int compare(char p1 [N], char p2 [M])
| ^~~~~~~
prog.cc:27:5: note: template argument deduction/substitution failed:
prog.cc:34:12: note: couldn't deduce template parameter 'N'
34 | compare("hi", "mom");
| ~~~~~~~^~~~~~~~~~~~~
Why doesn't this work?