#include <iostream>
using namespace std;
void f(const char* arg)
{
cout << "arg is a pointer" << endl;
}
template<size_t N>
void f(const char (&arg)[N])
{
cout << "arg is an array." << endl;
}
int main()
{
f("");
}
我的編譯器是叮噹3.8。爲什麼clang將字符串字面量當作指針而不是數組?
的輸出是:
arg是指向
然而,根據cppreference.com,
類型的前綴的字符串文字的是爲const char [] 。
爲什麼重載分辨率不像預期的那樣表現?
等效的例子,但抽象了模板:http://melpon.org/wandbox/permlink/0nGenu5Ysj40wS8u –
[密切相關](https://stackoverflow.com/questions/16708307/is-it-possible-to -legally-overload-a-string-literal-and-const-char),可能是重複。你怎麼看? –