用的编译器是 clang-700.1.81
#include <iostream>
using namespace std;
template <unsigned N, unsigned M>
void compare(const char p1[N], const char p2[M]) {
cout << N << ", " << M << endl;
}
int main() {
compare("hi", "hello");
return 0;
}
错误信息是:
test.cpp:9:3: error: no matching function for call to 'compare'
compare("hi", "hello");
^~~~~~~
test.cpp:5:6: note: candidate template ignored: couldn't infer template argument
'N'
void compare(char p1[N], char p2[M]) {
^
1 error generated.
请问这里为什么编译器推断不出 N 为 3, M 为 6 呢?
但是如果参数是用引用,那就可以了。
#include <iostream>
using namespace std;
template <unsigned N, unsigned M>
void compare(const char (&p1)[N], const char (&p2)[M]) {
cout << N << ", " << M << endl;
}
int main() {
compare("hi", "hello");
return 0;
}