C ++标准是否包括必须包含的标准标头? [英] Does the C++ standard include what standard headers must include?
问题描述
在visualstudio上,标题线程"包括以下所有标题:
On visualstudio the header "thread" includes all of the following headers:
#include <exception>
#include <iosfwd>
#include <functional>
#include <chrono>
#include <memory>
#include <tuple>
所以现在我们可以使用它:
So now we can just use this:
#include <thread>
using namespace std;
this_thread::sleep_for(1s);
因此,在VS上,您不必再次包含"chrono"就可以使用1s 1000ms等.我们是否可以假设所有平台上都包含?或更笼统地说,标准会说标准标头必须包含哪些标头?
So on VS you dont have to include "chrono" again to be able to use 1s 1000ms etc. Can we assume that always includes on all platforms? Or more general, does the standard say what headers standard headers must include?
推荐答案
不,没有这样的保证.该标准仅指示标头必须提供的定义.对于[ thread.threads ],您会发现摘要确实不包含任何 #include
.其他标头可能会有所不同,但仍然如此,仅需要列出的标头,请参见例如[位集].
No, there is no such guarantee. The standard only dictates, which definitions a header must provide. In the case of [thread.threads] you will find that the synopsis does not contain any #include
. This can be different for other headers but still then, only the listed ones are required, see e.g. [bitset].
例如,TDM GCC 4.9.2的 thread
头文件不包含诸如 iosfwd
或 exception
之类的头文件.
For example, the thread
header for TDM GCC 4.9.2 does not include header files such as iosfwd
or exception
.
作为一个明确的示例,以下代码在我的GCC 5上编译,而不在GCC 7上编译,因为在对标准库的更新中,GCC维护者决定 algorithm
不再包含 numeric
.
As an explicit example, the following compiles on my GCC 5 but not on GCC 7 because in an update to the standard library the GCC maintainers decided that algorithm
should no longer include numeric
.
#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});
}
话虽如此,您应该始终包括所有必需的顶级标题,这些标题提供了所需的符号.
That being said, you should always include the all required top-level headers which provide the symbols you need.
这篇关于C ++标准是否包括必须包含的标准标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!