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 50API reference 51------------- 52.. doxygenfile:: pw_polyfill/language_feature_macros.h 53 :sections: define 54 55------------------------------------------------ 56Backport new C++ features to older C++ standards 57------------------------------------------------ 58Pigweed backports a few C++ features to older C++ standards. These features are 59provided in the ``pw`` namespace. If the features are provided by the toolchain, 60the ``pw`` versions are aliases of the ``std`` versions. 61 62List of backported features 63=========================== 64 65These features are documented here, but are not implemented in ``pw_polyfill``. 66See the listed Pigweed module for more information on each feature. 67 68.. list-table:: 69 :header-rows: 1 70 71 * - Header 72 - Feature 73 - Pigweed module and header 74 - Polyfill name 75 * - ``<array>`` 76 - ``std::to_array`` 77 - :ref:`module-pw_containers`/to_array.h 78 - ``pw::containers::to_array`` 79 * - ``<bit>`` 80 - ``std::endian`` 81 - :ref:`module-pw_bytes`/bit.h 82 - ``pw::endian`` 83 * - ``<expected>`` 84 - ``std::expected`` 85 - :ref:`module-pw_result`/expected.h 86 - ``pw::expected`` 87 * - ``<span>`` 88 - ``std::span`` 89 - :ref:`module-pw_span`/span.h 90 - ``pw::span`` 91 92------------------------------------------------ 93Adapt code to compile with different C standards 94------------------------------------------------ 95 96.. _module-pw_polyfill-static_assert: 97 98static_assert 99============= 100 101``pw_polyfill/static_assert.h`` provides C23-style ``static_assert`` for older 102C standards. As in C23, the message argument is optional. 103 104.. literalinclude:: c_test.c 105 :start-after: [static_assert-example] 106 :end-before: [static_assert-example] 107 :language: c 108 109.. tip:: 110 111 Use ``pw_polyfill/static_assert.h`` rather than ``<assert.h>`` to provide 112 ``static_assert`` in C. ``<assert.h>`` provides a ``#define static_assert 113 _Static_assert`` macro prior to C23, but its ``static_assert`` does not 114 support C23 semantics (optional message argument), and it defines the 115 unwanted ``assert`` macro. 116 117------ 118Zephyr 119------ 120To enable ``pw_polyfill`` for Zephyr add ``CONFIG_PIGWEED_POLYFILL=y`` to the 121project's configuration. 122