如果我编写的新函数具有C库函数的签名,由于模棱两可,我预计会发生编译错误。但是,我不明白为什么下面的C ++代码没有错误。
#include <iostream>
#include <cmath>
using namespace std;
double sqrt(double number)
{
return number * 2;
}
int main( )
{
cout << sqrt(2.3) << endl;
cout << ::sqrt(2.3) << endl;
cout << std::sqrt(2.3) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我将sqrt()的返回类型更改为int,则由于在cmath中使用double sqrt()的声明含糊性,会发生编译错误。如何覆盖double sqrt()?(实际上,所有cmath函数都可以被覆盖,我不知道为什么。)
这个问题的后续问题这一个.
考虑以下程序:
#include <cmath>
// meaningless, only for language-lawyer purpose
void abs(void*) {}
int main(){
abs(nullptr);
}
Run Code Online (Sandbox Code Playgroud)
该程序是否导致未定义的行为?
标准中的相关部分是[extern.names]/4:
来自使用外部链接声明的C标准库的每个函数签名保留给实现,以用作具有extern"C"和extern"C++"链接的函数签名,或者作为全局命名空间中的命名空间范围的名称.
我不确定是否允许超载.