Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 973
T.C. Avatar asked Oct 28 '14 17:10

T.C.


People also ask

Can you include a header file in another header file C?

You need to use #include. In C, everything is explicit; don't expect it to work by magic.

What are the two header files in C?

There are two types of header files: the files that the programmer writes and the files that comes with your compiler. You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio. h header file, which comes along with your compiler.

What is C standard library and header files?

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.


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

like image 79
T.C. Avatar answered Sep 19 '22 06:09

T.C.


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
like image 23
rsjaffe Avatar answered Sep 22 '22 06:09

rsjaffe