xref: /openbsd-src/gnu/llvm/libcxx/docs/DesignDocs/HeaderRemovalPolicy.rst (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert=====================
2*4bdff4beSrobertHeader Removal Policy
3*4bdff4beSrobert=====================
4*4bdff4beSrobert
5*4bdff4beSrobertPolicy
6*4bdff4beSrobert------
7*4bdff4beSrobert
8*4bdff4beSrobertLibc++ is in the process of splitting larger headers into smaller modular
9*4bdff4beSrobertheaders. This makes it possible to remove these large headers from other
10*4bdff4beSrobertheaders. For example, instead of including ``<algorithm>`` entirely it is
11*4bdff4beSrobertpossible to only include the headers for the algorithms used. When the
12*4bdff4beSrobertStandard indirectly adds additional header includes, using the smaller headers
13*4bdff4beSrobertaids reducing the growth of top-level headers. For example ``<atomic>`` uses
14*4bdff4beSrobert``std::chrono::nanoseconds`` and included ``<chrono>``. In C++20 ``<chrono>``
15*4bdff4beSrobertrequires ``<format>`` which adds several other headers (like ``<string>``,
16*4bdff4beSrobert``<optional>``, ``<tuple>``) which are not needed in ``<atomic>``.
17*4bdff4beSrobert
18*4bdff4beSrobertThe benefit of using minimal headers is that the size of libc++'s top-level
19*4bdff4beSrobertheaders becomes smaller. This improves the compilation time when users include
20*4bdff4beSroberta top-level header. It also avoids header inclusion cycles and makes it easier
21*4bdff4beSrobertto port headers to platforms with reduced functionality.
22*4bdff4beSrobert
23*4bdff4beSrobertA disadvantage is that users unknowingly depend on these transitive includes.
24*4bdff4beSrobertThus removing an include might break their build after upgrading a newer
25*4bdff4beSrobertversion of libc++. For example, ``<algorithm>`` is often forgotten but using
26*4bdff4beSrobertalgorithms will still work through those transitive includes. This problem is
27*4bdff4beSrobertsolved by modules, however in practice most people do not use modules (yet).
28*4bdff4beSrobert
29*4bdff4beSrobertTo ease the removal of transitive includes in libc++, libc++ will remove
30*4bdff4beSrobertunnecessary transitive includes in newly supported C++ versions. This means
31*4bdff4beSrobertthat users will have to fix their missing includes in order to upgrade to a
32*4bdff4beSrobertnewer version of the Standard. Libc++ also reserves the right to remove
33*4bdff4beSroberttransitive includes at any other time, however new language versions will be
34*4bdff4beSrobertused as a convenient way to perform bulk removals of transitive includes.
35*4bdff4beSrobert
36*4bdff4beSrobertFor libc++ developers, this means that any transitive include removal must be
37*4bdff4beSrobertguarded by something of the form:
38*4bdff4beSrobert
39*4bdff4beSrobert.. code-block:: cpp
40*4bdff4beSrobert
41*4bdff4beSrobert   #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
42*4bdff4beSrobert   #  include <algorithm>
43*4bdff4beSrobert   #  include <iterator>
44*4bdff4beSrobert   #  include <utility>
45*4bdff4beSrobert   #endif
46*4bdff4beSrobert
47*4bdff4beSrobertWhen users define ``_LIBCPP_REMOVE_TRANSITIVE_INCLUDES``, libc++ will not
48*4bdff4beSrobertinclude transitive headers, regardless of the language version. This can be
49*4bdff4beSrobertuseful for users to aid the transition to a newer language version, or by users
50*4bdff4beSrobertwho simply want to make sure they include what they use in their code.
51*4bdff4beSrobert
52*4bdff4beSrobert
53*4bdff4beSrobertRationale
54*4bdff4beSrobert---------
55*4bdff4beSrobert
56*4bdff4beSrobertRemoving headers is not only an issue for software developers, but also for
57*4bdff4beSrobertvendors. When a vendor updates libc++ several of their upstream packages might
58*4bdff4beSrobertfail to compile, forcing them to fix these packages or file a bug with their
59*4bdff4beSrobertupstream packages. Usually upgrading software to a new language standard is
60*4bdff4beSrobertdone explicitly by software developers. This means they most likely will
61*4bdff4beSrobertdiscover and fix the missing includes, lessening the burden for the vendors.
62