blob: 25bcee043b8cbe534732fe3e0b623bc7cfc4f1c1 [file] [log] [blame]
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08001.. _chapter-pw-polyfill:
2
3.. default-domain:: cpp
4
5.. highlight:: sh
6
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -07007===========
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08008pw_polyfill
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -07009===========
10The ``pw_polyfill`` module backports new C++ features to older C++ standards.
11When possible, features are adapted to work with in standards as old as C++11.
12Pigweed does not support C++ standards older than C++11.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080013
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080014------------------------------------------------
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070015Backport new C++ features to older C++ standards
16------------------------------------------------
17The main purpose of ``pw_polyfill`` is to bring new C++ library and language
18features to older C++ standards. No additional ``#include`` statements are
19required to use these features; simply write code assuming that the features are
20available. This implicit feature backporting is provided through the
21``overrides`` library in the ``pw_polyfill`` module. GN automatically adds this
22library as a dependency in ``pw_source_set``.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080023
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070024``pw_polyfill`` backports C++ library features by wrapping the standard C++ and
25C headers. The wrapper headers include the original header using
26`#include_next <https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html>`_, then
27add missing features. The backported features are only defined if they aren't
28provided by the standard header, so ``pw_polyfill`` is safe to use when
29compiling with any standard C++11 or newer.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080030
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070031Language features are backported or stubbed via the
32``pw_polyfill/language_features.h`` header. This header is included in files
33that depend on ``pw_polyfill`` with GCC's ``-include`` option so that no
34``#include`` statement is required.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080035
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070036The wrapper headers are in ``public_overrides``. These are provided through the
37``"$dir_pw_polyfill:overrides"`` library, which the GN build adds as a
38dependency for all targets. To apply overrides in Bazel or CMake, depend on the
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080039``//pw_polyfill:overrides`` or ``pw_polyfill.overrides`` targets. In other build
40systems, add ``pw_polyfill/standard_library_public`` and
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070041``pw_polyfill/public_overrides`` as include paths, and add a ``-include`` option
42for the ``language_features.h`` header.
43
44Backported features
45===================
46================== ================================ ============================================ ========================================
47Header Feature Level of support Feature test macro
48================== ================================ ============================================ ========================================
49<array> std::to_array full __cpp_lib_to_array
Wyatt Heplerecf19232020-09-02 14:35:09 -070050<bit> std::endian full __cpp_lib_endian
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070051<cstdlib> std::byte full; some operators not constexpr in C++11 __cpp_lib_byte
52<iterator> std::data, std::size full __cpp_lib_nonmember_container_access
53<type_traits> \*_t trait aliases partial (can expand as needed) __cpp_lib_transformation_trait_aliases
54<type_traits> std::is_null_pointer full __cpp_lib_is_null_pointer
55<utilty> std::integer_sequence & helpers full __cpp_lib_integer_sequence
56(language feature) consteval keyword ignored (equivalent to constexpr) __cpp_consteval
57(language feature) constinit keyword supported in clang; ignored in GCC __cpp_constinit
58(language feature) static_assert with no message full __cpp_static_assert
59================== ================================ ============================================ ========================================
60
61----------------------------------------------------
62Adapt code to compile with different versions of C++
63----------------------------------------------------
64 ``pw_polyfill`` provides features for adapting to different C++ standards when
65 ``pw_polyfill:overrides``'s automatic backporting is insufficient:
66
67 - ``pw_polyfill/standard.h`` -- provides a macro for checking the C++ standard
68 - ``pw_polyfill/language_feature_macros.h`` -- provides macros for adapting
69 code to work with or without newer language features
70
71In GN, Bazel, or CMake, depend on ``$dir_pw_polyfill``, ``//pw_polyfill``,
72or ``pw_polyfill``, respectively, to access these features. In other build
73systems, add ``pw_polyfill/standard_library_public`` and
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080074``pw_polyfill/public_overrides`` as include paths.
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070075
76-------------
77Compatibility
78-------------
79C++11