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.
-
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
__cplusplusmacro.
Language feature macros#
Pigweed AI summary: The "pw_polyfill/language_feature_macros.h" file provides macros for adapting code to work with or without certain C++ language features. These macros include "PW_INLINE_VARIABLE" for inline variables, "PW_CONSTEXPR_CPP20" for constexpr if compiling for C++20, "PW_CONSTEVAL" for consteval if supported by the compiler, and "PW_CONSTINIT" for constinit in clang and GCC 10+. To access these features, depend on "pw_polyfill" in GN
pw_polyfill/language_feature_macros.h provides macros for adapting code to
work with or without C++ language features.
Macro |
Feature |
Description |
Feature test macro |
|---|---|---|---|
|
inline variables |
inline if supported by the compiler |
|
|
|
|
|
|
|
|
|
|
|
|
|
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 AI summary: Pigweed offers a few C++ features in the "pw" namespace that can be used in older C++ standards. If the toolchain provides these features, the "pw" versions are aliases of the "std" versions. Additionally, "pw_polyfill" backports some C++17 library features to C++14 by wrapping standard C++ and C headers. The backported features are only defined if they aren't provided by the standard header and can only be used when compiling with C
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, 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#
Pigweed AI summary: This paragraph describes a table of backported features, including the header, feature, feature test macro, module, polyfill header, and polyfill name. The table includes features such as std::to_array, std::endian, std::byte, std::expected, std::data, std::size, and std::span, along with their corresponding modules and polyfill names.
Header |
Feature |
Feature test macro |
Module |
Polyfill header |
Polyfill name |
|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pw_polyfill |
|
|
|
|
|
|
|
|
|
|
|
pw_polyfill |
|
|
|
|
|
|
|
Compatibility#
Pigweed AI summary: This paragraph discusses compatibility and specifically mentions C++14. It also provides instructions for enabling the pw_polyfill for Zephyr by adding CONFIG_PIGWEED_POLYFILL=y to the project's configuration.
C++14
Zephyr#
Pigweed AI summary: To use pw_polyfill in Zephyr, simply add CONFIG_PIGWEED_POLYFILL=y to the project's configuration.
To enable pw_polyfill for Zephyr add CONFIG_PIGWEED_POLYFILL=y to the
project’s configuration.