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?
-
3Why is this information useful? – Pradhan Oct 28 '14 at 21:48
-
6@Pradhan It's useful to know, for instance, that if you included `
` you don't have to include ` – T.C. Oct 28 '14 at 22:23`, or ` ` if you just need `std::begin`/`std::end`. -
1@T.C. But it is a bad idea to not to specify explicitly the header file in which a type that you use is declared. This will make the code more difficult to read even though it could make the compilation a little bit faster. Devs are not supposed to know the guaranteed includes. Well, for the case of std::begin, it is obvious, but for other cases, it may not. – Gab是好人 Dec 02 '16 at 14:17
-
Please *include what you use* at all times. There is only downsides to not do it even if it is just taking 30 seconds to explain to co-workers why you did it. It already took you more time explaining it than doing the right thing. – screwnut Jan 30 '21 at 18:16
2 Answers
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:
<utility>
(§23.2.1 [utility.syn])<string>
(§24.3.1 [string.syn])<array>
(§26.3.2 [array.syn])<deque>
(§26.3.3 [deque.syn])<forward_list>
(§26.3.4 [forward_list.syn])<list>
(§26.3.5 [list.syn])<vector>
(§26.3.6 [vector.syn])<map>
(§26.4.2 [associative.map.syn])<set>
(§26.4.3 [associative.set.syn])<unordered_map>
(§26.5.2 [unord.map.syn])<unordered_set>
(§26.5.3 [unord.set.syn])<queue>
(§26.6.2 [queue.syn])<stack>
(§26.6.3 [stack.syn])<algorithm>
(§28.2 [algorithms.syn])<random>
(§29.6.2 [rand.synopsis])<valarray>
(§29.7.1 [valarray.syn])<regex>
(§31.4 [re.syn])
<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]).

- 133,968
- 17
- 288
- 421
-
@ShafikYaghmour Only difference after comparing N3337 and N4140 is the lack of `std::cbegin` etc. in C++11. Most of this is in the synopsis for the respective headers. – T.C. Oct 28 '14 at 18:16
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
cinttypes is included in:
- cstdint

- 5,600
- 7
- 27
- 39
-
About your last point with cinttypes and cstdint, the paper you linked (29.12.2) defines it exactly the opposite: cinttypes is not included by cstdint, but cstdint is included by cinttypes. – jmk Sep 09 '22 at 10:56