Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard include what standard headers must include?

Tags:

c++

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);

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?

like image 272
Serve Laurijssen Avatar asked Mar 09 '23 09:03

Serve Laurijssen


1 Answers

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].

For example, the thread header for TDM GCC 4.9.2 does not include header files such as iosfwd or exception.

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.

like image 88
Henri Menke Avatar answered Apr 29 '23 19:04

Henri Menke