blob: 33b87a8a3a6587949064ccd30363d4ec808f5084 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_polyfill:
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08002
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -07003===========
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08004pw_polyfill
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -07005===========
6The ``pw_polyfill`` module backports new C++ features to older C++ standards.
7When possible, features are adapted to work with in standards as old as C++11.
8Pigweed does not support C++ standards older than C++11.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -08009
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080010------------------------------------------------
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070011Backport new C++ features to older C++ standards
12------------------------------------------------
13The main purpose of ``pw_polyfill`` is to bring new C++ library and language
14features to older C++ standards. No additional ``#include`` statements are
15required to use these features; simply write code assuming that the features are
16available. This implicit feature backporting is provided through the
17``overrides`` library in the ``pw_polyfill`` module. GN automatically adds this
18library as a dependency in ``pw_source_set``.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080019
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070020``pw_polyfill`` backports C++ library features by wrapping the standard C++ and
21C headers. The wrapper headers include the original header using
22`#include_next <https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html>`_, then
23add missing features. The backported features are only defined if they aren't
24provided by the standard header, so ``pw_polyfill`` is safe to use when
25compiling with any standard C++11 or newer.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080026
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070027Language features are backported or stubbed via the
28``pw_polyfill/language_features.h`` header. This header is included in files
29that depend on ``pw_polyfill`` with GCC's ``-include`` option so that no
30``#include`` statement is required.
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080031
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070032The wrapper headers are in ``public_overrides``. These are provided through the
33``"$dir_pw_polyfill:overrides"`` library, which the GN build adds as a
34dependency for all targets. To apply overrides in Bazel or CMake, depend on the
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080035``//pw_polyfill:overrides`` or ``pw_polyfill.overrides`` targets. In other build
36systems, add ``pw_polyfill/standard_library_public`` and
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070037``pw_polyfill/public_overrides`` as include paths, and add a ``-include`` option
38for the ``language_features.h`` header.
39
40Backported features
41===================
42================== ================================ ============================================ ========================================
43Header Feature Level of support Feature test macro
44================== ================================ ============================================ ========================================
45<array> std::to_array full __cpp_lib_to_array
Wyatt Heplerecf19232020-09-02 14:35:09 -070046<bit> std::endian full __cpp_lib_endian
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070047<cstdlib> std::byte full; some operators not constexpr in C++11 __cpp_lib_byte
48<iterator> std::data, std::size full __cpp_lib_nonmember_container_access
49<type_traits> \*_t trait aliases partial (can expand as needed) __cpp_lib_transformation_trait_aliases
50<type_traits> std::is_null_pointer full __cpp_lib_is_null_pointer
51<utilty> std::integer_sequence & helpers full __cpp_lib_integer_sequence
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070052(language feature) static_assert with no message full __cpp_static_assert
53================== ================================ ============================================ ========================================
54
55----------------------------------------------------
56Adapt code to compile with different versions of C++
57----------------------------------------------------
58 ``pw_polyfill`` provides features for adapting to different C++ standards when
59 ``pw_polyfill:overrides``'s automatic backporting is insufficient:
60
61 - ``pw_polyfill/standard.h`` -- provides a macro for checking the C++ standard
62 - ``pw_polyfill/language_feature_macros.h`` -- provides macros for adapting
63 code to work with or without newer language features
64
Wyatt Heplerc66851d2021-06-16 09:55:13 -070065
66Language feature macros
67=======================
68====================== ================================ ======================================== ==========================
69Macro Feature Description Feature test macro
70====================== ================================ ======================================== ==========================
71PW_INLINE_VARIABLE inline variables inline if supported by the compiler __cpp_inline_variables
72PW_CONSTEXPR_FUNCTION relaxed constexpr function rules constexpr if relaxed rules are supported __cpp_constexpr >= 201304L
73PW_CONSTEXPR_CPP20 constexpr in C++20 constexpr if compiling for C++20 __cplusplus >= 202002L
74PW_CONSTEVAL consteval consteval if supported by the compiler __cpp_consteval
75PW_CONSTINIT constinit constinit in clang and GCC 10+ __cpp_constinit
76====================== ================================ ======================================== ==========================
77
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070078In GN, Bazel, or CMake, depend on ``$dir_pw_polyfill``, ``//pw_polyfill``,
79or ``pw_polyfill``, respectively, to access these features. In other build
80systems, add ``pw_polyfill/standard_library_public`` and
Wyatt Heplerc542a5d2020-01-15 15:43:10 -080081``pw_polyfill/public_overrides`` as include paths.
Wyatt Hepler8e59f4d2020-08-27 10:34:21 -070082
83-------------
84Compatibility
85-------------
86C++11