aboutsummaryrefslogtreecommitdiff
path: root/pw_polyfill/docs.rst
blob: 419ac22aa12b8874daa8e57822c1acf1585f0544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.. _module-pw_polyfill:

===========
pw_polyfill
===========
The ``pw_polyfill`` module supports compiling code against different C++
standards. It also supports backporting a few C++17 features to C++14.

----------------------------------------------------
Adapt code to compile with different versions of C++
----------------------------------------------------

C++ standard macro
==================
``pw_polyfill/standard.h`` provides a macro for checking if a C++ standard is
supported.

.. c:macro:: PW_CXX_STANDARD_IS_SUPPORTED(standard)

   Evaluates true if the provided C++ standard (98, 11, 14, 17, 20) is supported
   by the compiler. This is a simpler, cleaner alternative to checking the value
   of the ``__cplusplus`` macro.

Language feature macros
=======================
``pw_polyfill/language_feature_macros.h`` provides macros for adapting code to
work with or without C++ language features.

.. list-table::
  :header-rows: 1

  * - Macro
    - Feature
    - Description
    - Feature test macro
  * - ``PW_INLINE_VARIABLE``
    - inline variables
    - inline if supported by the compiler
    - ``__cpp_inline_variables``
  * - ``PW_CONSTEXPR_CPP20``
    - ``constexpr``
    - ``constexpr`` if compiling for C++20
    - ``__cplusplus >= 202002L``
  * - ``PW_CONSTEVAL``
    - ``consteval``
    - ``consteval`` if supported by the compiler
    - ``__cpp_consteval``
  * - ``PW_CONSTINIT``
    - ``constinit``
    - ``constinit`` in clang and GCC 10+
    - ``__cpp_constinit``

In GN, Bazel, or CMake, depend on ``$dir_pw_polyfill``, ``//pw_polyfill``,
or ``pw_polyfill``, respectively, to access these features. In other build
systems, add ``pw_polyfill/public`` as an include path.

------------------------------------------------
Backport new C++ features to older C++ standards
------------------------------------------------
Pigweed backports a few C++ features to older C++ standards. These features
are provided in the ``pw`` namespace. If the features are provided by the
toolchain, the ``pw`` versions are aliases of the ``std`` versions.

``pw_polyfill`` also backports a few C++17 library features to C++14 by wrapping
the standard C++ and C headers. The wrapper headers include the original header
using `#include_next
<https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html>`_, then add missing
features. The backported features are only defined if they aren't provided by
the standard header and can only be used when compiling with C++14 in GN.

Backported features
===================
.. list-table::
  :header-rows: 1

  * - Header
    - Feature
    - Feature test macro
    - Module
    - Polyfill header
    - Polyfill name
  * - ``<array>``
    - ``std::to_array``
    - ``__cpp_lib_to_array``
    - :ref:`module-pw_containers`
    - ``pw_containers/to_array.h``
    - ``pw::containers::to_array``
  * - ``<bit>``
    - ``std::endian``
    - ``__cpp_lib_endian``
    - :ref:`module-pw_bytes`
    - ``pw_bytes/bit.h``
    - ``pw::endian``
  * - ``<cstdlib>``
    - ``std::byte``
    - ``__cpp_lib_byte``
    - pw_polyfill
    - ``<cstdlib>``
    - ``std::byte``
  * - ``<iterator>``
    - ``std::data``, ``std::size``
    - ``__cpp_lib_nonmember_container_access``
    - pw_polyfill
    - ``<iterator>``
    - ``std::data``, ``std::size``
  * - ``<span>``
    - ``std::span``
    - ``__cpp_lib_span``
    - :ref:`module-pw_span`
    - ``pw_span/span.h``
    - ``pw::span``

-------------
Compatibility
-------------
C++14

Zephyr
======
To enable ``pw_polyfill`` for Zephyr add ``CONFIG_PIGWEED_POLYFILL=y`` to the
project's configuration.