c++ - 遗留标准 C 库头文件和重载的 C++ 函数

标签 c++ legacy standard-library

D.5

中的 C++ 语言标准

2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

这似乎相当明确地说明了(“...每个名称...”,“...相同的声明...”)旧式 <name.h> header 必须提供与新式 <cname> 相同的声明集 header ,但在全局命名空间中。例如,各种 C 函数的特定于 C++ 的重载版本也不异常(exception)。

这似乎意味着 <math.h>必须提供三个版本的sin功能:sin(float) , sin(double)sin(long double)在全局命名空间中。反过来,这意味着以下 C++ 代码应该无法通过重载解析

#include <math.h>

int main() {
  sin(1);
}

它在 MSVC++ 编译器下确实失败,但在 GCC 和 Clang 下编译成功。那么,GCC 是否只是忽略了关于已弃用的旧式 header 的标准要求?还是我以某种方式误解了标准中的措辞?

最佳答案

感谢@hvd 的评论,我看到了曙光,事实证明 MSVC 是正确的,GCC 也应该提示歧义。

包括<cmath>之间的唯一区别对比<math.h>是名称最初的范围,在 namespace std 中对于前者,全局命名空间对于后者(实现也可以自由提供其他命名空间中的名称,但这不是强制性的),并且包含 .h 的事实C header 的变体已弃用。

关于c++ - 遗留标准 C 库头文件和重载的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791436/

相关文章:

macos - OS X Yosemite 更新后 IntelliJ 无法启动

c - `nextafter` 和 `nexttoward` : why this particular interface?

c++ - 如何在 Windows 64 位机器上将 C++ 应用程序编译为 x64 模式?

c++ 理解多线程中的 lock_guard 和 mutex

python - 转换遗留数据的文件格式

java - 使用类加载器隔离静态单例类

perl - 如何判断 Perl 模块是核心还是标准安装的一部分?

c - 重新实现库函数作为其他语言的实践

c++ - 为什么第二个功能不等待用户输入?

c++ - 在C++中将指向成员函数的指针传递给父级