1.. _module-pw_polyfill: 2 3=========== 4pw_polyfill 5=========== 6.. pigweed-module:: 7 :name: pw_polyfill 8 9The ``pw_polyfill`` module supports compiling code against different C++ 10standards. 11 12-------------------------------------------------- 13Adapt code to compile with different C++ standards 14-------------------------------------------------- 15 16C++ standard macro 17================== 18``pw_polyfill/standard.h`` provides macros for checking if a C++ or C standard 19is supported. 20 21.. doxygendefine:: PW_CXX_STANDARD_IS_SUPPORTED 22.. doxygendefine:: PW_C_STANDARD_IS_SUPPORTED 23 24Language feature macros 25======================= 26``pw_polyfill/language_feature_macros.h`` provides macros for adapting code to 27work with or without C++ language features. 28 29.. list-table:: 30 :header-rows: 1 31 32 * - Macro 33 - Feature 34 - Description 35 - Feature test macro 36 * - ``PW_CONSTEXPR_CPP20`` 37 - ``constexpr`` 38 - ``constexpr`` if compiling for C++20 39 - ``__cplusplus >= 202002L`` 40 * - ``PW_CONSTEVAL`` 41 - ``consteval`` 42 - ``consteval`` if supported by the compiler 43 - ``__cpp_consteval`` 44 * - ``PW_CONSTINIT`` 45 - ``constinit`` 46 - ``constinit`` in clang and GCC 10+ 47 - ``__cpp_constinit`` 48 49In GN, Bazel, or CMake, depend on ``$dir_pw_polyfill``, ``//pw_polyfill``, 50or ``pw_polyfill``, respectively, to access these features. In other build 51systems, add ``pw_polyfill/public`` as an include path. 52 53API reference 54------------- 55.. doxygenfile:: pw_polyfill/language_feature_macros.h 56 :sections: define 57 58------------------------------------------------ 59Backport new C++ features to older C++ standards 60------------------------------------------------ 61Pigweed backports a few C++ features to older C++ standards. These features are 62provided in the ``pw`` namespace. If the features are provided by the toolchain, 63the ``pw`` versions are aliases of the ``std`` versions. 64 65List of backported features 66=========================== 67 68These features are documented here, but are not implemented in ``pw_polyfill``. 69See the listed Pigweed module for more information on each feature. 70 71.. list-table:: 72 :header-rows: 1 73 74 * - Header 75 - Feature 76 - Pigweed module and header 77 - Polyfill name 78 * - ``<array>`` 79 - ``std::to_array`` 80 - :ref:`module-pw_containers`/to_array.h 81 - ``pw::containers::to_array`` 82 * - ``<bit>`` 83 - ``std::endian`` 84 - :ref:`module-pw_bytes`/bit.h 85 - ``pw::endian`` 86 * - ``<expected>`` 87 - ``std::expected`` 88 - :ref:`module-pw_result`/expected.h 89 - ``pw::expected`` 90 * - ``<span>`` 91 - ``std::span`` 92 - :ref:`module-pw_span`/span.h 93 - ``pw::span`` 94 95------------------------------------------------ 96Adapt code to compile with different C standards 97------------------------------------------------ 98 99.. _module-pw_polyfill-static_assert: 100 101static_assert 102============= 103 104``pw_polyfill/static_assert.h`` provides C23-style ``static_assert`` for older 105C standards. As in C23, the message argument is optional. 106 107.. literalinclude:: c_test.c 108 :start-after: [static_assert-example] 109 :end-before: [static_assert-example] 110 :language: c 111 112.. tip:: 113 114 Use ``pw_polyfill/static_assert.h`` rather than ``<assert.h>`` to provide 115 ``static_assert`` in C. ``<assert.h>`` provides a ``#define static_assert 116 _Static_assert`` macro prior to C23, but its ``static_assert`` does not 117 support C23 semantics (optional message argument), and it defines the 118 unwanted ``assert`` macro. 119 120------ 121Zephyr 122------ 123To enable ``pw_polyfill`` for Zephyr add ``CONFIG_PIGWEED_POLYFILL=y`` to the 124project's configuration. 125