用的编译器是 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;
}
1
yangff 2016-01-12 00:28:01 +08:00
大概是类型?
void fuck(const char p1[3]) { cout << typeid(p1).name(); } |
2
msg7086 2016-01-12 00:30:47 +08:00
帮你用 gcc 跑了一下,得出以下错误信息:
test.cpp:5:6: note: candidate: template<unsigned int N, unsigned int M> void compare(const char*, const char*) void compare(const char p1[N], const char p2[M]) { ^ 所以很明显不是引用的时候变成了 const char * 了。 |