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