Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 1 | .. _using-libcxx: |
| 2 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 3 | ============ |
| 4 | Using libc++ |
| 5 | ============ |
| 6 | |
| 7 | .. contents:: |
| 8 | :local: |
| 9 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 10 | Usually, libc++ is packaged and shipped by a vendor through some delivery vehicle |
| 11 | (operating system distribution, SDK, toolchain, etc) and users don't need to do |
| 12 | anything special in order to use the library. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 13 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 14 | This page contains information about configuration knobs that can be used by |
| 15 | users when they know libc++ is used by their toolchain, and how to use libc++ |
| 16 | when it is not the default library used by their toolchain. |
| 17 | |
| 18 | |
| 19 | Using a different version of the C++ Standard |
| 20 | ============================================= |
| 21 | |
| 22 | Libc++ implements the various versions of the C++ Standard. Changing the version of |
| 23 | the standard can be done by passing ``-std=c++XY`` to the compiler. Libc++ will |
| 24 | automatically detect what Standard is being used and will provide functionality that |
| 25 | matches that Standard in the library. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 26 | |
| 27 | .. code-block:: bash |
| 28 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 29 | $ clang++ -std=c++17 test.cpp |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 30 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 31 | .. warning:: |
| 32 | Using ``-std=c++XY`` with a version of the Standard that has not been ratified yet |
| 33 | is considered unstable. Libc++ reserves the right to make breaking changes to the |
| 34 | library until the standard has been ratified. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 35 | |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 | [diff] [blame] | 36 | |
Louis Dionne | 7300a65 | 2022-07-19 14:44:06 | [diff] [blame] | 37 | Enabling experimental C++ Library features |
| 38 | ========================================== |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 39 | |
Louis Dionne | 7300a65 | 2022-07-19 14:44:06 | [diff] [blame] | 40 | Libc++ provides implementations of some experimental features. Experimental features |
| 41 | are either Technical Specifications (TSes) or official features that were voted to |
| 42 | the Standard but whose implementation is not complete or stable yet in libc++. Those |
| 43 | are disabled by default because they are neither API nor ABI stable. However, the |
Louis Dionne | deb3b55 | 2022-07-20 14:42:04 | [diff] [blame] | 44 | ``-fexperimental-library`` compiler flag can be defined to turn those features on. |
Eric Fiselier | 539cd67 | 2016-05-03 22:32:08 | [diff] [blame] | 45 | |
| 46 | .. warning:: |
Louis Dionne | deb3b55 | 2022-07-20 14:42:04 | [diff] [blame] | 47 | Experimental libraries are experimental. |
Louis Dionne | 7300a65 | 2022-07-19 14:44:06 | [diff] [blame] | 48 | * The contents of the ``<experimental/...>`` headers and the associated static |
Eric Fiselier | 539cd67 | 2016-05-03 22:32:08 | [diff] [blame] | 49 | library will not remain compatible between versions. |
| 50 | * No guarantees of API or ABI stability are provided. |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 51 | * When the standardized version of an experimental feature is implemented, |
Louis Dionne | 776acf2 | 2019-06-11 14:48:40 | [diff] [blame] | 52 | the experimental feature is removed two releases after the non-experimental |
| 53 | version has shipped. The full policy is explained :ref:`here <experimental features>`. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 54 | |
Louis Dionne | deb3b55 | 2022-07-20 14:42:04 | [diff] [blame] | 55 | .. note:: |
| 56 | On compilers that do not support the ``-fexperimental-library`` flag, users can |
| 57 | define the ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro and manually link against the |
| 58 | appropriate static library (usually shipped as ``libc++experimental.a``) to get |
| 59 | access to experimental library features. |
| 60 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 61 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 62 | Using libc++ when it is not the system default |
| 63 | ============================================== |
| 64 | |
| 65 | On systems where libc++ is provided but is not the default, Clang provides a flag |
| 66 | called ``-stdlib=`` that can be used to decide which standard library is used. |
| 67 | Using ``-stdlib=libc++`` will select libc++: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 68 | |
| 69 | .. code-block:: bash |
| 70 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 71 | $ clang++ -stdlib=libc++ test.cpp |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 72 | |
Louis Dionne | ff7a332 | 2021-09-07 16:55:48 | [diff] [blame] | 73 | On systems where libc++ is the library in use by default such as macOS and FreeBSD, |
| 74 | this flag is not required. |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 75 | |
| 76 | |
| 77 | .. _alternate libcxx: |
| 78 | |
| 79 | Using a custom built libc++ |
| 80 | =========================== |
| 81 | |
| 82 | Most compilers provide a way to disable the default behavior for finding the |
| 83 | standard library and to override it with custom paths. With Clang, this can |
| 84 | be done with: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 85 | |
| 86 | .. code-block:: bash |
| 87 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 88 | $ clang++ -nostdinc++ -nostdlib++ \ |
| 89 | -isystem <install>/include/c++/v1 \ |
| 90 | -L <install>/lib \ |
| 91 | -Wl,-rpath,<install>/lib \ |
| 92 | -lc++ \ |
| 93 | test.cpp |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 94 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 95 | The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path, |
| 96 | which causes the system's dynamic linker to look for libc++ in ``<install>/lib`` |
| 97 | whenever the program is loaded. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 98 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 99 | GCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs`` |
| 100 | instead. Since that removes all the standard system libraries and not just libc++, |
| 101 | the system libraries must be re-added manually. For example: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 102 | |
| 103 | .. code-block:: bash |
| 104 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 105 | $ g++ -nostdinc++ -nodefaultlibs \ |
| 106 | -isystem <install>/include/c++/v1 \ |
| 107 | -L <install>/lib \ |
| 108 | -Wl,-rpath,<install>/lib \ |
| 109 | -lc++ -lc++abi -lm -lc -lgcc_s -lgcc \ |
| 110 | test.cpp |
Eric Fiselier | 19352b1 | 2016-01-20 01:26:30 | [diff] [blame] | 111 | |
| 112 | |
| 113 | GDB Pretty printers for libc++ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 114 | ============================== |
Eric Fiselier | 19352b1 | 2016-01-20 01:26:30 | [diff] [blame] | 115 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 116 | GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does |
| 117 | provide pretty-printers itself. Those can be used as: |
Eric Fiselier | 19352b1 | 2016-01-20 01:26:30 | [diff] [blame] | 118 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 119 | .. code-block:: bash |
Eric Fiselier | 19352b1 | 2016-01-20 01:26:30 | [diff] [blame] | 120 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 121 | $ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \ |
| 122 | -ex "python register_libcxx_printer_loader()" \ |
| 123 | <args> |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 124 | |
Christopher Di Bella | ab46648 | 2022-11-17 07:36:37 | [diff] [blame] | 125 | .. _include-what-you-use: |
| 126 | |
| 127 | include-what-you-use (IWYU) |
| 128 | =========================== |
| 129 | |
| 130 | libc++ provides an IWYU `mapping file <https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUMappings.md>`, |
| 131 | which drastically improves the accuracy of the tool when using libc++. To use the mapping file with |
| 132 | IWYU, you should run the tool like so: |
| 133 | |
| 134 | .. code-block:: bash |
| 135 | |
| 136 | $ include-what-you-use -Xiwyu /path/to/libcxx/include/libcxx.imp file.cpp |
| 137 | |
| 138 | If you would prefer to not use that flag, then you can replace ``/path/to/include-what-you-use/share/libcxx.imp``` |
| 139 | file with the libc++-provided ``libcxx.imp`` file. |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 140 | |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 141 | .. _termination-handler: |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 142 | |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 143 | Overriding the default termination handler |
| 144 | ========================================== |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 145 | |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 146 | When the library wants to terminate due to an unforeseen condition (such as a hardening assertion |
| 147 | failure), the program is aborted through a special verbose termination function. The library provides |
| 148 | a default function that prints an error message and calls ``std::abort()``. Note that this function is |
| 149 | provided by the static or shared library, so it is only available when deploying to a platform where |
| 150 | the compiled library is sufficiently recent. On older platforms, the program will terminate in an |
| 151 | unspecified unsuccessful manner, but the quality of diagnostics won't be great. |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 152 | |
Louis Dionne | 17c05a4 | 2023-01-09 21:59:59 | [diff] [blame] | 153 | However, users can also override that mechanism at two different levels. First, the mechanism can be |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 154 | overridden at compile time by defining the ``_LIBCPP_VERBOSE_ABORT(format, args...)`` variadic macro. |
Louis Dionne | 17c05a4 | 2023-01-09 21:59:59 | [diff] [blame] | 155 | When that macro is defined, it will be called with a format string as the first argument, followed by |
| 156 | a series of arguments to format using printf-style formatting. Compile-time customization may be |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 157 | useful to get precise control over code generation, however it is also inconvenient to use in |
Louis Dionne | 17c05a4 | 2023-01-09 21:59:59 | [diff] [blame] | 158 | some cases. Indeed, compile-time customization of the verbose termination function requires that all |
| 159 | translation units be compiled with a consistent definition for ``_LIBCPP_VERBOSE_ABORT`` to avoid ODR |
| 160 | violations, which can add complexity in the build system of users. |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 161 | |
Louis Dionne | 17c05a4 | 2023-01-09 21:59:59 | [diff] [blame] | 162 | Otherwise, if compile-time customization is not necessary, link-time customization of the handler is also |
| 163 | possible, similarly to how replacing ``operator new`` works. This mechanism trades off fine-grained control |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 164 | over the call site where the termination is initiated in exchange for better ergonomics. Link-time |
| 165 | customization is done by simply defining the following function in exactly one translation unit of your |
| 166 | program: |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 167 | |
| 168 | .. code-block:: cpp |
| 169 | |
Louis Dionne | 507125a | 2022-07-25 17:43:47 | [diff] [blame] | 170 | void __libcpp_verbose_abort(char const* format, ...) |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 171 | |
| 172 | This mechanism is similar to how one can replace the default definition of ``operator new`` |
| 173 | and ``operator delete``. For example: |
| 174 | |
| 175 | .. code-block:: cpp |
| 176 | |
| 177 | // In HelloWorldHandler.cpp |
Louis Dionne | 507125a | 2022-07-25 17:43:47 | [diff] [blame] | 178 | #include <version> // must include any libc++ header before defining the function (C compatibility headers excluded) |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 179 | |
Louis Dionne | 507125a | 2022-07-25 17:43:47 | [diff] [blame] | 180 | void std::__libcpp_verbose_abort(char const* format, ...) { |
Louis Dionne | 7de5aca | 2022-07-25 17:19:51 | [diff] [blame] | 181 | va_list list; |
| 182 | va_start(list, format); |
| 183 | std::vfprintf(stderr, format, list); |
| 184 | va_end(list); |
| 185 | |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 186 | std::abort(); |
| 187 | } |
| 188 | |
| 189 | // In HelloWorld.cpp |
| 190 | #include <vector> |
| 191 | |
| 192 | int main() { |
| 193 | std::vector<int> v; |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 194 | int& x = v[0]; // Your termination function will be called here if hardening is enabled. |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 195 | } |
| 196 | |
Louis Dionne | 507125a | 2022-07-25 17:43:47 | [diff] [blame] | 197 | Also note that the verbose termination function should never return. Since assertions in libc++ |
| 198 | catch undefined behavior, your code will proceed with undefined behavior if your function is called |
| 199 | and does return. |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 200 | |
Louis Dionne | 507125a | 2022-07-25 17:43:47 | [diff] [blame] | 201 | Furthermore, exceptions should not be thrown from the function. Indeed, many functions in the |
| 202 | library are ``noexcept``, and any exception thrown from the termination function will result |
| 203 | in ``std::terminate`` being called. |
Louis Dionne | b0fd949 | 2022-03-03 22:37:03 | [diff] [blame] | 204 | |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 205 | Libc++ Configuration Macros |
| 206 | =========================== |
| 207 | |
| 208 | Libc++ provides a number of configuration macros which can be used to enable |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 209 | or disable extended libc++ behavior, including enabling hardening or thread |
| 210 | safety annotations. |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 211 | |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 212 | **_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**: |
| 213 | This macro is used to enable -Wthread-safety annotations on libc++'s |
Jennifer Chukwu | 21bef4e | 2021-04-17 15:04:06 | [diff] [blame] | 214 | ``std::mutex`` and ``std::lock_guard``. By default, these annotations are |
Eric Fiselier | efd48ca | 2016-11-13 23:00:30 | [diff] [blame] | 215 | disabled and must be manually enabled by the user. |
Shoaib Meenai | fc6100c | 2016-12-05 19:40:12 | [diff] [blame] | 216 | |
varconst | f0dfe68 | 2023-07-14 23:58:15 | [diff] [blame] | 217 | **_LIBCPP_ENABLE_HARDENED_MODE**: |
| 218 | This macro is used to enable the :ref:`hardened mode <using-hardened-mode>`. |
| 219 | |
| 220 | **_LIBCPP_ENABLE_DEBUG_MODE**: |
| 221 | This macro is used to enable the :ref:`debug mode <using-hardened-mode>`. |
| 222 | |
Shoaib Meenai | fc6100c | 2016-12-05 19:40:12 | [diff] [blame] | 223 | **_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**: |
| 224 | This macro is used to disable all visibility annotations inside libc++. |
| 225 | Defining this macro and then building libc++ with hidden visibility gives a |
| 226 | build of libc++ which does not export any symbols, which can be useful when |
| 227 | building statically for inclusion into another library. |
Eric Fiselier | bd68825 | 2016-12-08 23:57:08 | [diff] [blame] | 228 | |
Eric Fiselier | b1e7a12 | 2017-01-13 22:02:08 | [diff] [blame] | 229 | **_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**: |
| 230 | This macro disables the additional diagnostics generated by libc++ using the |
| 231 | `diagnose_if` attribute. These additional diagnostics include checks for: |
| 232 | |
Louis Dionne | 3560fbf3 | 2018-12-06 21:46:17 | [diff] [blame] | 233 | * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_` |
| 234 | counterparts a comparator which is not const callable. |
| 235 | * Giving an unordered associative container a hasher that is not const |
| 236 | callable. |
Eric Fiselier | b1e7a12 | 2017-01-13 22:02:08 | [diff] [blame] | 237 | |
Shoaib Meenai | 492d713 | 2017-10-09 19:25:17 | [diff] [blame] | 238 | **_LIBCPP_NO_VCRUNTIME**: |
| 239 | Microsoft's C and C++ headers are fairly entangled, and some of their C++ |
| 240 | headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled |
| 241 | in from a lot of other headers and provides definitions which clash with |
| 242 | libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so |
| 243 | there's no way for libc++ to provide a compatible definition, since you can't |
| 244 | have multiple definitions). |
| 245 | |
| 246 | By default, libc++ solves this problem by deferring to Microsoft's vcruntime |
| 247 | headers where needed. However, it may be undesirable to depend on vcruntime |
| 248 | headers, since they may not always be available in cross-compilation setups, |
| 249 | or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro |
| 250 | prevents libc++ from depending on vcruntime headers. Consequently, it also |
| 251 | prevents libc++ headers from being interoperable with vcruntime headers (from |
| 252 | the aforementioned clashes), so users of this macro are promising to not |
| 253 | attempt to combine libc++ headers with the problematic vcruntime headers. This |
| 254 | macro also currently prevents certain `operator new`/`operator delete` |
| 255 | replacement scenarios from working, e.g. replacing `operator new` and |
| 256 | expecting a non-replaced `operator new[]` to call the replaced `operator new`. |
| 257 | |
Nikolas Klauser | 3c355e2 | 2022-09-01 10:02:58 | [diff] [blame] | 258 | **_LIBCPP_DISABLE_NODISCARD_EXT**: |
| 259 | This macro disables library-extensions of ``[[nodiscard]]``. |
Nikolas Klauser | b978dfb | 2022-08-19 13:41:56 | [diff] [blame] | 260 | See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for more information. |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 261 | |
Louis Dionne | a470a13 | 2019-03-12 20:10:06 | [diff] [blame] | 262 | **_LIBCPP_DISABLE_DEPRECATION_WARNINGS**: |
| 263 | This macro disables warnings when using deprecated components. For example, |
| 264 | using `std::auto_ptr` when compiling in C++11 mode will normally trigger a |
| 265 | warning saying that `std::auto_ptr` is deprecated. If the macro is defined, |
| 266 | no warning will be emitted. By default, this macro is not defined. |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 267 | |
Eric Fiselier | 2a1bfa9 | 2017-02-17 03:25:08 | [diff] [blame] | 268 | C++17 Specific Configuration Macros |
| 269 | ----------------------------------- |
| 270 | **_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**: |
| 271 | This macro is used to re-enable all the features removed in C++17. The effect |
| 272 | is equivalent to manually defining each macro listed below. |
| 273 | |
Eric Fiselier | 07e93d3 | 2017-02-17 03:30:25 | [diff] [blame] | 274 | **_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**: |
Arthur O'Dwyer | d42d9e1 | 2021-05-24 22:36:17 | [diff] [blame] | 275 | This macro is used to re-enable `auto_ptr`. |
| 276 | |
| 277 | **_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**: |
| 278 | This macro is used to re-enable the `binder1st`, `binder2nd`, |
| 279 | `pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`, |
| 280 | `mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`, |
| 281 | `const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t` |
| 282 | class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`, |
| 283 | and `ptr_fun` functions. |
| 284 | |
| 285 | **_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**: |
| 286 | This macro is used to re-enable the `random_shuffle` algorithm. |
| 287 | |
| 288 | **_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**: |
| 289 | This macro is used to re-enable `set_unexpected`, `get_unexpected`, and |
| 290 | `unexpected`. |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 291 | |
Mark de Wever | 8aa5965 | 2022-07-07 17:07:03 | [diff] [blame] | 292 | C++20 Specific Configuration Macros |
| 293 | ----------------------------------- |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 294 | **_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**: |
| 295 | This macro can be used to disable diagnostics emitted from functions marked |
| 296 | ``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` |
| 297 | for more information. |
| 298 | |
Arthur O'Dwyer | d42d9e1 | 2021-05-24 22:36:17 | [diff] [blame] | 299 | **_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**: |
| 300 | This macro is used to re-enable all the features removed in C++20. The effect |
| 301 | is equivalent to manually defining each macro listed below. |
| 302 | |
| 303 | **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**: |
| 304 | This macro is used to re-enable redundant members of `allocator<T>`, |
| 305 | including `pointer`, `reference`, `rebind`, `address`, `max_size`, |
| 306 | `construct`, `destroy`, and the two-argument overload of `allocate`. |
Arthur O'Dwyer | d42d9e1 | 2021-05-24 22:36:17 | [diff] [blame] | 307 | |
Ilya Biryukov | 374f938 | 2022-06-15 08:55:55 | [diff] [blame] | 308 | **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**: |
| 309 | This macro is used to re-enable the library-provided specializations of |
| 310 | `allocator<void>` and `allocator<const void>`. |
| 311 | Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS` |
| 312 | to ensure that removed members of `allocator<void>` can be accessed. |
| 313 | |
Arthur O'Dwyer | dc06688 | 2021-05-25 18:34:18 | [diff] [blame] | 314 | **_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**: |
| 315 | This macro is used to re-enable the `argument_type`, `result_type`, |
| 316 | `first_argument_type`, and `second_argument_type` members of class |
| 317 | templates such as `plus`, `logical_not`, `hash`, and `owner_less`. |
| 318 | |
Arthur O'Dwyer | d42d9e1 | 2021-05-24 22:36:17 | [diff] [blame] | 319 | **_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**: |
| 320 | This macro is used to re-enable `not1`, `not2`, `unary_negate`, |
| 321 | and `binary_negate`. |
| 322 | |
| 323 | **_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**: |
| 324 | This macro is used to re-enable `raw_storage_iterator`. |
| 325 | |
wmbat | 2ff5a56 | 2021-07-02 17:08:36 | [diff] [blame] | 326 | **_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**: |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 327 | This macro is used to re-enable `is_literal_type`, `is_literal_type_v`, |
wmbat | 2ff5a56 | 2021-07-02 17:08:36 | [diff] [blame] | 328 | `result_of` and `result_of_t`. |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 329 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 330 | |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 331 | Libc++ Extensions |
| 332 | ================= |
| 333 | |
| 334 | This section documents various extensions provided by libc++, how they're |
| 335 | provided, and any information regarding how to use them. |
| 336 | |
| 337 | .. _nodiscard extension: |
| 338 | |
| 339 | Extended applications of ``[[nodiscard]]`` |
| 340 | ------------------------------------------ |
| 341 | |
| 342 | The ``[[nodiscard]]`` attribute is intended to help users find bugs where |
| 343 | function return values are ignored when they shouldn't be. After C++17 the |
| 344 | C++ standard has started to declared such library functions as ``[[nodiscard]]``. |
| 345 | However, this application is limited and applies only to dialects after C++17. |
| 346 | Users who want help diagnosing misuses of STL functions may desire a more |
| 347 | liberal application of ``[[nodiscard]]``. |
| 348 | |
| 349 | For this reason libc++ provides an extension that does just that! The |
Nikolas Klauser | 3c355e2 | 2022-09-01 10:02:58 | [diff] [blame] | 350 | extension is enabled by default and can be disabled by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``. |
| 351 | The extended applications of ``[[nodiscard]]`` takes two forms: |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 352 | |
| 353 | 1. Backporting ``[[nodiscard]]`` to entities declared as such by the |
| 354 | standard in newer dialects, but not in the present one. |
| 355 | |
Arthur O'Dwyer | 4b7bad9 | 2021-04-05 18:56:03 | [diff] [blame] | 356 | 2. Extended applications of ``[[nodiscard]]``, at the library's discretion, |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 357 | applied to entities never declared as such by the standard. |
| 358 | |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 359 | Entities declared with ``_LIBCPP_NODISCARD_EXT`` |
| 360 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 361 | |
| 362 | This section lists all extended applications of ``[[nodiscard]]`` to entities |
| 363 | which no dialect declares as such (See the second form described above). |
| 364 | |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 365 | * ``adjacent_find`` |
| 366 | * ``all_of`` |
| 367 | * ``any_of`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 368 | * ``as_const`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 369 | * ``binary_search`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 370 | * ``bit_cast`` |
Nikolas Klauser | 7d3bba5 | 2023-06-13 14:57:26 | [diff] [blame] | 371 | * ``bit_ceil`` |
| 372 | * ``bit_floor`` |
| 373 | * ``bit_width`` |
| 374 | * ``byteswap`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 375 | * ``cbrt`` |
| 376 | * ``ceil`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 377 | * ``clamp`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 378 | * ``copysign`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 379 | * ``count_if`` |
| 380 | * ``count`` |
Nikolas Klauser | 7d3bba5 | 2023-06-13 14:57:26 | [diff] [blame] | 381 | * ``countl_zero`` |
| 382 | * ``countl_one`` |
| 383 | * ``countr_zero`` |
| 384 | * ``countr_one`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 385 | * ``equal_range`` |
| 386 | * ``equal`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 387 | * ``fabs`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 388 | * ``find_end`` |
| 389 | * ``find_first_of`` |
| 390 | * ``find_if_not`` |
| 391 | * ``find_if`` |
| 392 | * ``find`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 393 | * ``floor`` |
| 394 | * ``fmax`` |
| 395 | * ``fmin`` |
| 396 | * ``forward`` |
| 397 | * ``fpclassify`` |
Roman Lebedev | c65d39a | 2018-09-22 17:54:48 | [diff] [blame] | 398 | * ``get_temporary_buffer`` |
Nikolas Klauser | 7d3bba5 | 2023-06-13 14:57:26 | [diff] [blame] | 399 | * ``has_single_bit`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 400 | * ``identity::operator()`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 401 | * ``includes`` |
| 402 | * ``is_heap_until`` |
| 403 | * ``is_heap`` |
| 404 | * ``is_partitioned`` |
| 405 | * ``is_permutation`` |
| 406 | * ``is_sorted_until`` |
| 407 | * ``is_sorted`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 408 | * ``isfinite`` |
| 409 | * ``isgreater`` |
| 410 | * ``isgreaterequal`` |
| 411 | * ``isinf`` |
| 412 | * ``isless`` |
| 413 | * ``islessequal`` |
| 414 | * ``islessgreater`` |
| 415 | * ``isnan`` |
| 416 | * ``isnormal`` |
| 417 | * ``isunordered`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 418 | * ``lexicographical_compare`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 419 | * ``lock_guard``'s constructors |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 420 | * ``lower_bound`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 421 | * ``make_format_args`` |
| 422 | * ``make_wformat_args`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 423 | * ``max_element`` |
| 424 | * ``max`` |
| 425 | * ``min_element`` |
| 426 | * ``min`` |
| 427 | * ``minmax_element`` |
| 428 | * ``minmax`` |
| 429 | * ``mismatch`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 430 | * ``move_if_noexcept`` |
| 431 | * ``move`` |
| 432 | * ``nearbyint`` |
Nico Weber | 1362d7e | 2019-04-03 18:13:08 | [diff] [blame] | 433 | * ``none_of`` |
Nikolas Klauser | 7d3bba5 | 2023-06-13 14:57:26 | [diff] [blame] | 434 | * ``popcount`` |
Nikolas Klauser | 660b243 | 2022-11-01 19:06:11 | [diff] [blame] | 435 | * ``ranges::adjacent_find`` |
| 436 | * ``ranges::all_of`` |
| 437 | * ``ranges::any_of`` |
| 438 | * ``ranges::binary_search`` |
| 439 | * ``ranges::clamp`` |
| 440 | * ``ranges::count_if`` |
| 441 | * ``ranges::count`` |
| 442 | * ``ranges::equal_range`` |
| 443 | * ``ranges::equal`` |
| 444 | * ``ranges::find_end`` |
| 445 | * ``ranges::find_first_of`` |
| 446 | * ``ranges::find_if_not`` |
| 447 | * ``ranges::find_if`` |
| 448 | * ``ranges::find`` |
| 449 | * ``ranges::get_temporary_buffer`` |
| 450 | * ``ranges::includes`` |
| 451 | * ``ranges::is_heap_until`` |
| 452 | * ``ranges::is_heap`` |
| 453 | * ``ranges::is_partitioned`` |
| 454 | * ``ranges::is_permutation`` |
| 455 | * ``ranges::is_sorted_until`` |
| 456 | * ``ranges::is_sorted`` |
| 457 | * ``ranges::lexicographical_compare`` |
| 458 | * ``ranges::lower_bound`` |
| 459 | * ``ranges::max_element`` |
| 460 | * ``ranges::max`` |
| 461 | * ``ranges::min_element`` |
| 462 | * ``ranges::min`` |
| 463 | * ``ranges::minmax_element`` |
| 464 | * ``ranges::minmax`` |
| 465 | * ``ranges::mismatch`` |
| 466 | * ``ranges::none_of`` |
| 467 | * ``ranges::remove_if`` |
| 468 | * ``ranges::remove`` |
| 469 | * ``ranges::search_n`` |
| 470 | * ``ranges::search`` |
| 471 | * ``ranges::unique`` |
| 472 | * ``ranges::upper_bound`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 473 | * ``remove_if`` |
| 474 | * ``remove`` |
Nikolas Klauser | 633927d | 2022-12-25 19:13:39 | [diff] [blame] | 475 | * ``rint`` |
| 476 | * ``round`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 477 | * ``search_n`` |
| 478 | * ``search`` |
| 479 | * ``signbit`` |
| 480 | * ``to_integer`` |
| 481 | * ``to_underlying`` |
Nikolas Klauser | 633927d | 2022-12-25 19:13:39 | [diff] [blame] | 482 | * ``trunc`` |
Mark de Wever | 9c053e6 | 2023-06-04 12:37:20 | [diff] [blame] | 483 | * ``unique`` |
| 484 | * ``upper_bound`` |
| 485 | * ``vformat`` |
Louis Dionne | 07e984b | 2022-06-01 19:25:14 | [diff] [blame] | 486 | |
Louis Dionne | 497705f | 2022-08-10 21:34:45 | [diff] [blame] | 487 | Extended integral type support |
| 488 | ------------------------------ |
| 489 | |
| 490 | Several platforms support types that are not specified in the Standard, such as |
| 491 | the 128-bit integral types ``__int128_t`` and ``__uint128_t``. As an extension, |
| 492 | libc++ does a best-effort attempt to support these types like other integral |
| 493 | types, by supporting them notably in: |
| 494 | |
| 495 | * ``<bits>`` |
| 496 | * ``<charconv>`` |
| 497 | * ``<functional>`` |
| 498 | * ``<type_traits>`` |
| 499 | * ``<format>`` |
| 500 | * ``<random>`` |
| 501 | |
Louis Dionne | 07e984b | 2022-06-01 19:25:14 | [diff] [blame] | 502 | Additional types supported in random distributions |
| 503 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 504 | |
| 505 | The `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number |
| 506 | distributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is |
| 507 | undefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``, |
| 508 | ``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution`` |
| 509 | with ``int8_t``, ``__int128_t`` and their unsigned versions. |
Mark de Wever | 77ccf63 | 2022-07-07 18:02:07 | [diff] [blame] | 510 | |
Mark de Wever | f712775 | 2022-07-15 05:42:17 | [diff] [blame] | 511 | Extensions to ``<format>`` |
| 512 | -------------------------- |
| 513 | |
| 514 | The exposition only type ``basic-format-string`` and its typedefs |
| 515 | ``format-string`` and ``wformat-string`` became ``basic_format_string``, |
| 516 | ``format_string``, and ``wformat_string`` in C++23. Libc++ makes these types |
| 517 | available in C++20 as an extension. |
Mark de Wever | 68c3d66 | 2023-02-21 16:33:56 | [diff] [blame] | 518 | |
| 519 | For padding Unicode strings the ``format`` library relies on the Unicode |
| 520 | Standard. Libc++ retroactively updates the Unicode Standard in older C++ |
| 521 | versions. This allows the library to have better estimates for newly introduced |
| 522 | Unicode code points, without requiring the user to use the latest C++ version |
| 523 | in their code base. |
Mark de Wever | 7da669d | 2023-06-27 16:39:59 | [diff] [blame] | 524 | |
Mark de Wever | a9e5773 | 2022-04-03 15:43:52 | [diff] [blame] | 525 | In C++26 formatting pointers gained a type ``P`` and allows to use |
| 526 | zero-padding. These options have been retroactively applied to C++20. |
| 527 | |
Mark de Wever | 44d17cd | 2023-07-09 15:54:11 | [diff] [blame] | 528 | .. _turning-off-asan: |
| 529 | |
Advenam Tacet | 2fa1bec | 2023-05-04 21:16:06 | [diff] [blame] | 530 | Turning off ASan annotation in containers |
Mark de Wever | 7da669d | 2023-06-27 16:39:59 | [diff] [blame] | 531 | ----------------------------------------- |
Advenam Tacet | 2fa1bec | 2023-05-04 21:16:06 | [diff] [blame] | 532 | |
| 533 | ``__asan_annotate_container_with_allocator`` is a customization point to allow users to disable |
| 534 | `Address Sanitizer annotations for containers <https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow>`_ for specific allocators. This may be necessary for allocators that access allocated memory. |
| 535 | This customization point exists only when ``_LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS`` Feature Test Macro is defined. |
| 536 | |
| 537 | For allocators not running destructors, it is also possible to `bulk-unpoison memory <https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning>`_ instead of disabling annotations altogether. |
| 538 | |
| 539 | The struct may be specialized for user-defined allocators. It is a `Cpp17UnaryTypeTrait <http://eel.is/c++draft/type.traits#meta.rqmts>`_ with a base characteristic of ``true_type`` if the container is allowed to use annotations and ``false_type`` otherwise. |
| 540 | |
| 541 | The annotations for a ``user_allocator`` can be disabled like this: |
| 542 | |
| 543 | .. code-block:: cpp |
| 544 | |
| 545 | #ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS |
| 546 | template <class T> |
| 547 | struct std::__asan_annotate_container_with_allocator<user_allocator<T>> : std::false_type {}; |
| 548 | #endif |
| 549 | |
| 550 | Why may I want to turn it off? |
Mark de Wever | 7da669d | 2023-06-27 16:39:59 | [diff] [blame] | 551 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Advenam Tacet | 2fa1bec | 2023-05-04 21:16:06 | [diff] [blame] | 552 | |
| 553 | There are a few reasons why you may want to turn off annotations for an allocator. |
| 554 | Unpoisoning may not be an option, if (for example) you are not maintaining the allocator. |
| 555 | |
| 556 | * You are using allocator, which does not call destructor during deallocation. |
| 557 | * You are aware that memory allocated with an allocator may be accessed, even when unused by container. |
Martin Storsjö | fcbbd96 | 2023-03-15 10:11:28 | [diff] [blame] | 558 | |
| 559 | Platform specific behavior |
| 560 | ========================== |
| 561 | |
| 562 | Windows |
| 563 | ------- |
| 564 | |
| 565 | The ``stdout``, ``stderr``, and ``stdin`` file streams can be placed in |
| 566 | Unicode mode by a suitable call to ``_setmode()``. When in this mode, |
| 567 | the sequence of bytes read from, or written to, these streams is interpreted |
| 568 | as a sequence of little-endian ``wchar_t`` elements. Thus, use of |
| 569 | ``std::cout``, ``std::cerr``, or ``std::cin`` with streams in Unicode mode |
| 570 | will not behave as they usually do since bytes read or written won't be |
| 571 | interpreted as individual ``char`` elements. However, ``std::wcout``, |
| 572 | ``std::wcerr``, and ``std::wcin`` will behave as expected. |
| 573 | |
| 574 | Wide character stream such as ``std::wcin`` or ``std::wcout`` imbued with a |
| 575 | locale behave differently than they otherwise do. By default, wide character |
| 576 | streams don't convert wide characters but input/output them as is. If a |
| 577 | specific locale is imbued, the IO with the underlying stream happens with |
| 578 | regular ``char`` elements, which are converted to/from wide characters |
| 579 | according to the locale. Note that this doesn't behave as expected if the |
| 580 | stream has been set in Unicode mode. |