T.C.
T.C.

Reputation: 137330

Which headers in the C++ standard library are guaranteed to include another header?

The C++ standard library headers may include each other in unspecified ways, so programmers generally shouldn't depend on one header including another. In a few cases, however, a header is guaranteed to include another header, or make available certain functions that would otherwise require inclusion of another header. What are those cases?

Upvotes: 40

Views: 2831

Answers (2)

rsjaffe
rsjaffe

Reputation: 5730

Here are the mandatory includes for C++20, taken from N4860.

compare is included in:

  • array
  • chrono
  • coroutine
  • deque
  • filesystem
  • forward_list
  • iterator
  • list
  • map
  • memory
  • optional
  • queue
  • ranges
  • regex
  • set
  • stack
  • string
  • string_view
  • system_error
  • thread
  • tuple
  • typeindex
  • unordered_map
  • unordered_set
  • utility
  • variant
  • vector

initializer_list is included in:

  • algorithm
  • array
  • deque
  • forward_list
  • list
  • map
  • queue
  • random
  • ranges
  • regex
  • set
  • stack
  • string
  • thread
  • unordered_map
  • unordered_set
  • utility
  • valarray
  • vector

string is included in:

  • bitset

iosfwd is included in:

  • bitset
  • ios

concepts is included in:

  • iterator

iterator is included in:

  • ranges

ios, streambuf, istream are included in:

  • iostream

ostream is included in:

  • iostream
  • syncstream

cstdint is included in:

  • cinttypes

Upvotes: 3

T.C.
T.C.

Reputation: 137330

This answer ignores C headers - both the <meow.h> and <cmeow> ones. Of the C++ library headers (all references are to N4659):

<initializer_list> is guaranteed to be included by:

<iostream> is guaranteed to include <ios>, <streambuf>, <istream>, and <ostream> (§30.4.1 [iostream.syn]).

<ios> is guaranteed to include <iosfwd> (§30.5.1 [ios.syn]).

<bitset> is guaranteed to include <string> and <iosfwd> (§23.9.1 [bitset.syn]).

The free function templates std::begin, std::end, the C++14 c-, r-, and cr- versions, and the C++17 free function templates std::size, std::empty and std::data nominally reside in <iterator>, but are also available if any of the following headers is included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector> (§27.7 [iterator.range], §27.8 [iterator.container]).

When <string_view> is included, the *begin and *end functions, and the two generic std::swap overloads defined in [utility.swap] (swap(T&, T&) and swap(T (&a)[N], T (&b)[N])) are guaranteed to be available. size/empty/data, however, are not. (§24.4.1 [string.view.synop]).

Upvotes: 41

Related Questions