任何一個可以幫助我瞭解下面的代碼通參考數組中的C++
#include <iostream>
void foo(const char * c)
{
std::cout << "const char *" << std::endl;
}
template <size_t N>
void foo(const char (&t) [N])
{
std::cout << "array ref" << std::endl;
std::cout << sizeof(t) << std::endl;
}
int main()
{
const char t[34] = {'1'};
foo(t);
char d[34] = {'1'};
foo(d);
}
輸出是
const char *
array ref
34
爲什麼第一個foo的調用const char *
版本?我怎樣才能讓它調用參考版本?
msvc輸出爲'const char *,const char *' – Marlon 2011-03-02 21:14:22
輸出如他所說,'const char *,array ref,34'with'gcc-4.3.4'(http:// ideone.com/ejyCS)。 – James 2011-03-02 21:18:06
@ user511274 - 有趣的問題:) – 2011-03-02 21:19:51