C++ C++;标准头必须包括哪些标准头?

C++ C++;标准头必须包括哪些标准头?,c++,C++,在visualstudio上,标题“线程”包括以下所有标题: #include <exception> #include <iosfwd> #include <functional> #include <chrono> #include <memory> #include <tuple> #包括 #包括 #包括 #包括 #包括 #包括 现在我们可以用这个: #include <thread> using na

在visualstudio上,标题“线程”包括以下所有标题:

#include <exception>
#include <iosfwd>
#include <functional>
#include <chrono>
#include <memory>
#include <tuple>
#包括
#包括
#包括
#包括
#包括
#包括
现在我们可以用这个:

#include <thread>
using namespace std;
this_thread::sleep_for(1s);
#包括
使用名称空间std;
此线程::睡眠时间为(1s);
所以在VS中,你不必再次加入“chrono”就可以使用1s 1000ms等。
我们是否可以假设所有平台上都包含此功能?或者更一般地说,标准是否规定了标准标题必须包括哪些标题?

不,没有这样的保证。该标准仅规定了标题必须提供的定义。在[]的情况下,您会发现概要中不包含任何
#include
。对于其他标题,这可能有所不同,但仍然需要列出的标题,请参见例如[]

例如,TDM GCC 4.9.2的
线程
头文件不包括头文件,例如
iosfwd
异常

作为一个明确的例子,下面的代码是在我的GCC 5上编译的,但不是在GCC 7上编译的,因为在对标准库的更新中,GCC维护人员决定
算法
不应再包括
数值

#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> v = {1,2,3,4};
    int sum = std::accumulate(std::begin(v), std::end(v), int{0});
}
#包括
#包括
int main()
{
向量v={1,2,3,4};
int sum=std::accumulate(std::begin(v),std::end(v),int{0});
}


也就是说,您应该始终包含提供所需符号的所有必需的顶级标题。

不,没有这样的保证。该标准只定义了一个报头必须提供的信息,例如,你应该总是把你所编写的代码所需的所有头都包含在自己的代码中,而不依赖于嵌套的。我相信每个C++编译器都有自己的标准库,所以头文件将包括他们想要的任何东西。例如,TDM GCC的
thread
头不包括
iosfwd
exception
头。您可以自己查找:对……我这样做了,但无法找到它并不意味着它不在那里