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