gpt4 book ai didi

c++ - 在 Xcode 中,当我包含 时,stdlib.h 似乎也包含在内。为什么?

转载 作者:行者123 更新时间:2023-11-30 03:55:20 24 4
gpt4 key购买 nike

我对使用 Xcode 时包含 iostream 有疑问。

包含iostream后,似乎stdlib.h也被自动包含了,因为当我写“merg”时,< Xcode 的 strong>Code Completion 给了我函数“mergesort”,根据 Xcode 的文档stdlib.h 中的函数/强>。但我没有明确包含 stdlib.h。所以我猜是 iostream 的包含导致了 stdlib.h 的包含。谁能告诉我为什么?

按照C标准,stdlib.h没有mergesort之类的功能,为什么在Xcode中,stdlib.h有这样的功能?是不是说明stdlib.h中的Xcode 不是标准的吗?

如何获取每个头文件的依赖关系映射,例如,对于头文件example.h,我如何知道其他.h文件包含在此 exemple.h 中,其他 .h 文件本身包含文件 exemple.h。

很抱歉这个问题很长,非常感谢您的回答!

最佳答案

简而言之:它是实现定义的。

这意味着它取决于特定的标准库实现。该标准明确允许标准库头文件包含其他标准库头文件(或至少定义其中的符号); C++11 17.6.5.2/1:

A C++ header may include other C++ headers. A C++ header shall provide the declarations and definitions that appear in its synopsis. A C++ header shown in its synopsis as including other C++ headers shall provide the declarations and definitions that appear in the synopses of those other headers.

为了保持代码的可移植性(即使只是在同一编译器/标准库的不同版本之间),您应该遵循以下规则:

  1. 不要依赖传递包含。始终明确包含您需要的所有 header 。

  2. 不要对可传递的包含感到惊讶。它们是合法的。

关于c++ - 在 Xcode 中,当我包含 <iostream> 时,stdlib.h 似乎也包含在内。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29095324/

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