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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With