[英]Resolution of overloaded extern C function with default arguments
這段代碼:
#include <stdlib.h> // int abs(int);
int abs(int i = 0) { return 42; }
int main() {
return abs(1); // Returns 42
}
返回42
。
編譯器選擇重載的 C++ function。 我在許多版本的 g++/clang 上對此進行了測試。 我可以依靠這種行為嗎? 它在任何地方都有記錄嗎?
通過這樣做,您會得到未定義的行為。
[extern.names]
4 Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both
extern "C"
andextern "C++"
linkage, or as a name of namespace scope in the global namespace.
int abs(int)
正是這樣一個 function 簽名。 您在這里踩到標准庫,程序的行為是未定義的。
您可能不會在全局命名空間中定義這樣的abs
function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.