Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_polyfill: |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 2 | |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 3 | =========== |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 4 | pw_polyfill |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 5 | =========== |
| 6 | The ``pw_polyfill`` module backports new C++ features to older C++ standards. |
| 7 | When possible, features are adapted to work with in standards as old as C++11. |
| 8 | Pigweed does not support C++ standards older than C++11. |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 9 | |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 10 | ------------------------------------------------ |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 11 | Backport new C++ features to older C++ standards |
| 12 | ------------------------------------------------ |
| 13 | The main purpose of ``pw_polyfill`` is to bring new C++ library and language |
| 14 | features to older C++ standards. No additional ``#include`` statements are |
| 15 | required to use these features; simply write code assuming that the features are |
| 16 | available. This implicit feature backporting is provided through the |
| 17 | ``overrides`` library in the ``pw_polyfill`` module. GN automatically adds this |
| 18 | library as a dependency in ``pw_source_set``. |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 19 | |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 20 | ``pw_polyfill`` backports C++ library features by wrapping the standard C++ and |
| 21 | C headers. The wrapper headers include the original header using |
| 22 | `#include_next <https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html>`_, then |
| 23 | add missing features. The backported features are only defined if they aren't |
| 24 | provided by the standard header, so ``pw_polyfill`` is safe to use when |
| 25 | compiling with any standard C++11 or newer. |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 26 | |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 27 | Language features are backported or stubbed via the |
| 28 | ``pw_polyfill/language_features.h`` header. This header is included in files |
| 29 | that depend on ``pw_polyfill`` with GCC's ``-include`` option so that no |
| 30 | ``#include`` statement is required. |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 31 | |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 32 | The wrapper headers are in ``public_overrides``. These are provided through the |
| 33 | ``"$dir_pw_polyfill:overrides"`` library, which the GN build adds as a |
| 34 | dependency for all targets. To apply overrides in Bazel or CMake, depend on the |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 35 | ``//pw_polyfill:overrides`` or ``pw_polyfill.overrides`` targets. In other build |
| 36 | systems, add ``pw_polyfill/standard_library_public`` and |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 37 | ``pw_polyfill/public_overrides`` as include paths, and add a ``-include`` option |
| 38 | for the ``language_features.h`` header. |
| 39 | |
| 40 | Backported features |
| 41 | =================== |
| 42 | ================== ================================ ============================================ ======================================== |
| 43 | Header Feature Level of support Feature test macro |
| 44 | ================== ================================ ============================================ ======================================== |
| 45 | <array> std::to_array full __cpp_lib_to_array |
Wyatt Hepler | ecf1923 | 2020-09-02 14:35:09 -0700 | [diff] [blame] | 46 | <bit> std::endian full __cpp_lib_endian |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 47 | <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 Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 52 | (language feature) static_assert with no message full __cpp_static_assert |
| 53 | ================== ================================ ============================================ ======================================== |
| 54 | |
| 55 | ---------------------------------------------------- |
| 56 | Adapt 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 Hepler | c66851d | 2021-06-16 09:55:13 -0700 | [diff] [blame] | 65 | |
| 66 | Language feature macros |
| 67 | ======================= |
| 68 | ====================== ================================ ======================================== ========================== |
| 69 | Macro Feature Description Feature test macro |
| 70 | ====================== ================================ ======================================== ========================== |
| 71 | PW_INLINE_VARIABLE inline variables inline if supported by the compiler __cpp_inline_variables |
| 72 | PW_CONSTEXPR_FUNCTION relaxed constexpr function rules constexpr if relaxed rules are supported __cpp_constexpr >= 201304L |
| 73 | PW_CONSTEXPR_CPP20 constexpr in C++20 constexpr if compiling for C++20 __cplusplus >= 202002L |
| 74 | PW_CONSTEVAL consteval consteval if supported by the compiler __cpp_consteval |
| 75 | PW_CONSTINIT constinit constinit in clang and GCC 10+ __cpp_constinit |
| 76 | ====================== ================================ ======================================== ========================== |
| 77 | |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 78 | In GN, Bazel, or CMake, depend on ``$dir_pw_polyfill``, ``//pw_polyfill``, |
| 79 | or ``pw_polyfill``, respectively, to access these features. In other build |
| 80 | systems, add ``pw_polyfill/standard_library_public`` and |
Wyatt Hepler | c542a5d | 2020-01-15 15:43:10 -0800 | [diff] [blame] | 81 | ``pw_polyfill/public_overrides`` as include paths. |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 82 | |
| 83 | ------------- |
| 84 | Compatibility |
| 85 | ------------- |
| 86 | C++11 |