因爲您已在全局命名空間中重寫它。例如,如果您不希望轉到更安全,更乾淨的語言(如Nim),則使用名稱空間可避免此危險。
Proper use of namespace demo:
#include <iostream>
#include <cmath> // Uses ::log, which would be the log() here if it were not in a namespace, see http://stackoverflow.com/questions/11892976/why-is-my-log-in-the-std-namespace
// Silently overrides std::log
//double log(double d) { return 420; }
namespace uniquename {
using namespace std; // So we don't have to waste space on std:: when not needed.
double log(double d) {
return 42;
}
int main() {
cout << "Our log: " << log(4.2) << endl;
cout << "Standard log: " << std::log(4.2);
return 0;
}
}
// Global wrapper for our contained code.
int main() {
return uniquename::main();
}
輸出:
Our log: 42
Standard log: 1.43508
聽起來像一個嚴重的編譯器缺陷... – MFH 2012-08-09 22:38:53
我的MacPorts下重現此對G ++ 4.6。儘管如此,它在g ++ 4.2或4.4中不會發生。 – carlosdc 2012-08-09 22:39:24
http://codepad.org/Uwhgrv7q http://codepad.org/z07Cffyn Frome這兩個我會說std :: log()函數調用log()。但它會產生一個錯誤/警告你的文件重新定義日誌或類似的東西 – Gir 2012-08-09 22:42:33