gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:59 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com