Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 1 | .. _using-libcxx: |
| 2 | |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 3 | ============ |
| 4 | Using libc++ |
| 5 | ============ |
| 6 | |
| 7 | .. contents:: |
| 8 | :local: |
| 9 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [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 | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 13 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [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 | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 26 | |
| 27 | .. code-block:: bash |
| 28 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 29 | $ clang++ -std=c++17 test.cpp |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 30 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [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 | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 35 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 36 | |
Louis Dionne | 3d895a1 | 2022-07-19 10:44:06 -0400 | [diff] [blame] | 37 | Enabling experimental C++ Library features |
| 38 | ========================================== |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 39 | |
Louis Dionne | 3d895a1 | 2022-07-19 10:44:06 -0400 | [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 |
| 44 | ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro can be defined to turn those features on. Note |
| 45 | that you will also need to link to the appropriate ``libc++experimental.a`` static |
| 46 | archive. |
Eric Fiselier | fa43a5c | 2016-05-03 22:32:08 +0000 | [diff] [blame] | 47 | |
| 48 | .. warning:: |
| 49 | Experimental libraries are Experimental. |
Louis Dionne | 3d895a1 | 2022-07-19 10:44:06 -0400 | [diff] [blame] | 50 | * The contents of the ``<experimental/...>`` headers and the associated static |
Eric Fiselier | fa43a5c | 2016-05-03 22:32:08 +0000 | [diff] [blame] | 51 | library will not remain compatible between versions. |
| 52 | * No guarantees of API or ABI stability are provided. |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 53 | * When the standardized version of an experimental feature is implemented, |
Louis Dionne | 3767713 | 2019-06-11 14:48:40 +0000 | [diff] [blame] | 54 | the experimental feature is removed two releases after the non-experimental |
| 55 | version has shipped. The full policy is explained :ref:`here <experimental features>`. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 56 | |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 57 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 58 | Using libc++ when it is not the system default |
| 59 | ============================================== |
| 60 | |
| 61 | On systems where libc++ is provided but is not the default, Clang provides a flag |
| 62 | called ``-stdlib=`` that can be used to decide which standard library is used. |
| 63 | Using ``-stdlib=libc++`` will select libc++: |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 64 | |
| 65 | .. code-block:: bash |
| 66 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 67 | $ clang++ -stdlib=libc++ test.cpp |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 68 | |
Louis Dionne | e474551 | 2021-09-07 12:55:48 -0400 | [diff] [blame] | 69 | On systems where libc++ is the library in use by default such as macOS and FreeBSD, |
| 70 | this flag is not required. |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 71 | |
| 72 | |
| 73 | .. _alternate libcxx: |
| 74 | |
| 75 | Using a custom built libc++ |
| 76 | =========================== |
| 77 | |
| 78 | Most compilers provide a way to disable the default behavior for finding the |
| 79 | standard library and to override it with custom paths. With Clang, this can |
| 80 | be done with: |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 81 | |
| 82 | .. code-block:: bash |
| 83 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 84 | $ clang++ -nostdinc++ -nostdlib++ \ |
| 85 | -isystem <install>/include/c++/v1 \ |
| 86 | -L <install>/lib \ |
| 87 | -Wl,-rpath,<install>/lib \ |
| 88 | -lc++ \ |
| 89 | test.cpp |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 90 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 91 | The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path, |
| 92 | which causes the system's dynamic linker to look for libc++ in ``<install>/lib`` |
| 93 | whenever the program is loaded. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 94 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 95 | GCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs`` |
| 96 | instead. Since that removes all the standard system libraries and not just libc++, |
| 97 | the system libraries must be re-added manually. For example: |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 98 | |
| 99 | .. code-block:: bash |
| 100 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 101 | $ g++ -nostdinc++ -nodefaultlibs \ |
| 102 | -isystem <install>/include/c++/v1 \ |
| 103 | -L <install>/lib \ |
| 104 | -Wl,-rpath,<install>/lib \ |
| 105 | -lc++ -lc++abi -lm -lc -lgcc_s -lgcc \ |
| 106 | test.cpp |
Eric Fiselier | 41ee431 | 2016-01-20 01:26:30 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | GDB Pretty printers for libc++ |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 110 | ============================== |
Eric Fiselier | 41ee431 | 2016-01-20 01:26:30 +0000 | [diff] [blame] | 111 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 112 | GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does |
| 113 | provide pretty-printers itself. Those can be used as: |
Eric Fiselier | 41ee431 | 2016-01-20 01:26:30 +0000 | [diff] [blame] | 114 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 115 | .. code-block:: bash |
Eric Fiselier | 41ee431 | 2016-01-20 01:26:30 +0000 | [diff] [blame] | 116 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 117 | $ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \ |
| 118 | -ex "python register_libcxx_printer_loader()" \ |
| 119 | <args> |
Eric Fiselier | b382530 | 2016-11-13 23:00:30 +0000 | [diff] [blame] | 120 | |
| 121 | |
Louis Dionne | 6e8eb55 | 2022-03-03 17:37:03 -0500 | [diff] [blame] | 122 | .. _assertions-mode: |
| 123 | |
| 124 | Enabling the "safe libc++" mode |
| 125 | =============================== |
| 126 | |
| 127 | Libc++ contains a number of assertions whose goal is to catch undefined behavior in the |
| 128 | library, usually caused by precondition violations. Those assertions do not aim to be |
| 129 | exhaustive -- instead they aim to provide a good balance between safety and performance. |
| 130 | In particular, these assertions do not change the complexity of algorithms. However, they |
| 131 | might, in some cases, interfere with compiler optimizations. |
| 132 | |
| 133 | By default, these assertions are turned off. Vendors can decide to turn them on while building |
| 134 | the compiled library by defining ``LIBCXX_ENABLE_ASSERTIONS=ON`` at CMake configuration time. |
| 135 | When ``LIBCXX_ENABLE_ASSERTIONS`` is used, the compiled library will be built with assertions |
| 136 | enabled, **and** user code will be built with assertions enabled by default. If |
| 137 | ``LIBCXX_ENABLE_ASSERTIONS=OFF`` at CMake configure time, the compiled library will not contain |
| 138 | assertions and the default when building user code will be to have assertions disabled. |
| 139 | As a user, you can consult your vendor to know whether assertions are enabled by default. |
| 140 | |
| 141 | Furthermore, independently of any vendor-selected default, users can always control whether |
| 142 | assertions are enabled in their code by defining ``_LIBCPP_ENABLE_ASSERTIONS=0|1`` before |
| 143 | including any libc++ header (we recommend passing ``-D_LIBCPP_ENABLE_ASSERTIONS=X`` to the |
| 144 | compiler). Note that if the compiled library was built by the vendor without assertions, |
| 145 | functions compiled inside the static or shared library won't have assertions enabled even |
| 146 | if the user defines ``_LIBCPP_ENABLE_ASSERTIONS=1`` (the same is true for the inverse case |
| 147 | where the static or shared library was compiled **with** assertions but the user tries to |
| 148 | disable them). However, most of the code in libc++ is in the headers, so the user-selected |
| 149 | value for ``_LIBCPP_ENABLE_ASSERTIONS`` (if any) will usually be respected. |
| 150 | |
| 151 | When an assertion fails, an assertion handler function is called. The library provides a default |
| 152 | assertion handler that prints an error message and calls ``std::abort()``. Note that this assertion |
| 153 | handler is provided by the static or shared library, so it is only available when deploying to a |
| 154 | platform where the compiled library is sufficiently recent. However, users can also override that |
| 155 | assertion handler with their own, which can be useful to provide custom behavior, or when deploying |
| 156 | to older platforms where the default assertion handler isn't available. |
| 157 | |
| 158 | Replacing the default assertion handler is done by defining the following function: |
| 159 | |
| 160 | .. code-block:: cpp |
| 161 | |
| 162 | void __libcpp_assertion_handler(char const* file, int line, char const* expression, char const* message) |
| 163 | |
| 164 | This mechanism is similar to how one can replace the default definition of ``operator new`` |
| 165 | and ``operator delete``. For example: |
| 166 | |
| 167 | .. code-block:: cpp |
| 168 | |
| 169 | // In HelloWorldHandler.cpp |
Louis Dionne | b4fce35 | 2022-03-25 12:55:36 -0400 | [diff] [blame] | 170 | #include <version> // must include any libc++ header before defining the handler (C compatibility headers excluded) |
Louis Dionne | 6e8eb55 | 2022-03-03 17:37:03 -0500 | [diff] [blame] | 171 | |
| 172 | void std::__libcpp_assertion_handler(char const* file, int line, char const* expression, char const* message) { |
| 173 | std::printf("Assertion %s failed at %s:%d, more info: %s", expression, file, line, message); |
| 174 | std::abort(); |
| 175 | } |
| 176 | |
| 177 | // In HelloWorld.cpp |
| 178 | #include <vector> |
| 179 | |
| 180 | int main() { |
| 181 | std::vector<int> v; |
| 182 | int& x = v[0]; // Your assertion handler will be called here if _LIBCPP_ENABLE_ASSERTIONS=1 |
| 183 | } |
| 184 | |
| 185 | Also note that the assertion handler should usually not return. Since the assertions in libc++ |
| 186 | catch undefined behavior, your code will proceed with undefined behavior if your assertion |
| 187 | handler is called and does return. |
| 188 | |
| 189 | Furthermore, throwing an exception from the assertion handler is not recommended. Indeed, many |
| 190 | functions in the library are ``noexcept``, and any exception thrown from the assertion handler |
| 191 | will result in ``std::terminate`` being called. |
| 192 | |
| 193 | Back-deploying with a custom assertion handler |
| 194 | ---------------------------------------------- |
| 195 | When deploying to an older platform that does not provide a default assertion handler, the |
| 196 | compiler will diagnose the usage of ``std::__libcpp_assertion_handler`` with an error. This |
| 197 | is done to avoid the load-time error that would otherwise happen if the code was being deployed |
| 198 | on the older system. |
| 199 | |
| 200 | If you are providing a custom assertion handler, this error is effectively a false positive. |
| 201 | To let the library know that you are providing a custom assertion handler in back-deployment |
| 202 | scenarios, you must define the ``_LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED`` macro, |
| 203 | and the library will assume that you are providing your own definition. If no definition is |
| 204 | provided and the code is back-deployed to the older platform, it will fail to load when the |
| 205 | dynamic linker fails to find a definition for ``std::__libcpp_assertion_handler``, so you |
| 206 | should only remove the guard rails if you really mean it! |
| 207 | |
Eric Fiselier | b382530 | 2016-11-13 23:00:30 +0000 | [diff] [blame] | 208 | Libc++ Configuration Macros |
| 209 | =========================== |
| 210 | |
| 211 | Libc++ provides a number of configuration macros which can be used to enable |
| 212 | or disable extended libc++ behavior, including enabling "debug mode" or |
| 213 | thread safety annotations. |
| 214 | |
Eric Fiselier | b382530 | 2016-11-13 23:00:30 +0000 | [diff] [blame] | 215 | **_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**: |
| 216 | This macro is used to enable -Wthread-safety annotations on libc++'s |
Jennifer Chukwu | b978809 | 2021-04-17 20:34:06 +0530 | [diff] [blame] | 217 | ``std::mutex`` and ``std::lock_guard``. By default, these annotations are |
Eric Fiselier | b382530 | 2016-11-13 23:00:30 +0000 | [diff] [blame] | 218 | disabled and must be manually enabled by the user. |
Shoaib Meenai | f36e988 | 2016-12-05 19:40:12 +0000 | [diff] [blame] | 219 | |
| 220 | **_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**: |
| 221 | This macro is used to disable all visibility annotations inside libc++. |
| 222 | Defining this macro and then building libc++ with hidden visibility gives a |
| 223 | build of libc++ which does not export any symbols, which can be useful when |
| 224 | building statically for inclusion into another library. |
Eric Fiselier | 41b686e | 2016-12-08 23:57:08 +0000 | [diff] [blame] | 225 | |
Eric Fiselier | a7a14ed | 2017-01-13 22:02:08 +0000 | [diff] [blame] | 226 | **_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**: |
| 227 | This macro disables the additional diagnostics generated by libc++ using the |
| 228 | `diagnose_if` attribute. These additional diagnostics include checks for: |
| 229 | |
Louis Dionne | 878a3a8 | 2018-12-06 21:46:17 +0000 | [diff] [blame] | 230 | * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_` |
| 231 | counterparts a comparator which is not const callable. |
| 232 | * Giving an unordered associative container a hasher that is not const |
| 233 | callable. |
Eric Fiselier | a7a14ed | 2017-01-13 22:02:08 +0000 | [diff] [blame] | 234 | |
Shoaib Meenai | cfd1960 | 2017-10-09 19:25:17 +0000 | [diff] [blame] | 235 | **_LIBCPP_NO_VCRUNTIME**: |
| 236 | Microsoft's C and C++ headers are fairly entangled, and some of their C++ |
| 237 | headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled |
| 238 | in from a lot of other headers and provides definitions which clash with |
| 239 | libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so |
| 240 | there's no way for libc++ to provide a compatible definition, since you can't |
| 241 | have multiple definitions). |
| 242 | |
| 243 | By default, libc++ solves this problem by deferring to Microsoft's vcruntime |
| 244 | headers where needed. However, it may be undesirable to depend on vcruntime |
| 245 | headers, since they may not always be available in cross-compilation setups, |
| 246 | or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro |
| 247 | prevents libc++ from depending on vcruntime headers. Consequently, it also |
| 248 | prevents libc++ headers from being interoperable with vcruntime headers (from |
| 249 | the aforementioned clashes), so users of this macro are promising to not |
| 250 | attempt to combine libc++ headers with the problematic vcruntime headers. This |
| 251 | macro also currently prevents certain `operator new`/`operator delete` |
| 252 | replacement scenarios from working, e.g. replacing `operator new` and |
| 253 | expecting a non-replaced `operator new[]` to call the replaced `operator new`. |
| 254 | |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 255 | **_LIBCPP_ENABLE_NODISCARD**: |
| 256 | Allow the library to add ``[[nodiscard]]`` attributes to entities not specified |
| 257 | as ``[[nodiscard]]`` by the current language dialect. This includes |
| 258 | backporting applications of ``[[nodiscard]]`` from newer dialects and |
| 259 | additional extended applications at the discretion of the library. All |
| 260 | additional applications of ``[[nodiscard]]`` are disabled by default. |
| 261 | See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for |
| 262 | more information. |
| 263 | |
| 264 | **_LIBCPP_DISABLE_NODISCARD_EXT**: |
| 265 | This macro prevents the library from applying ``[[nodiscard]]`` to entities |
| 266 | purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` |
| 267 | for more information. |
| 268 | |
Louis Dionne | ded5b77 | 2019-03-12 20:10:06 +0000 | [diff] [blame] | 269 | **_LIBCPP_DISABLE_DEPRECATION_WARNINGS**: |
| 270 | This macro disables warnings when using deprecated components. For example, |
| 271 | using `std::auto_ptr` when compiling in C++11 mode will normally trigger a |
| 272 | warning saying that `std::auto_ptr` is deprecated. If the macro is defined, |
| 273 | no warning will be emitted. By default, this macro is not defined. |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 274 | |
Eric Fiselier | ddd7779 | 2017-02-17 03:25:08 +0000 | [diff] [blame] | 275 | C++17 Specific Configuration Macros |
| 276 | ----------------------------------- |
| 277 | **_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**: |
| 278 | This macro is used to re-enable all the features removed in C++17. The effect |
| 279 | is equivalent to manually defining each macro listed below. |
| 280 | |
Eric Fiselier | 65d5b4c | 2017-02-17 03:30:25 +0000 | [diff] [blame] | 281 | **_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**: |
Arthur O'Dwyer | a6b5107 | 2021-05-24 18:36:17 -0400 | [diff] [blame] | 282 | This macro is used to re-enable `auto_ptr`. |
| 283 | |
| 284 | **_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**: |
| 285 | This macro is used to re-enable the `binder1st`, `binder2nd`, |
| 286 | `pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`, |
| 287 | `mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`, |
| 288 | `const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t` |
| 289 | class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`, |
| 290 | and `ptr_fun` functions. |
| 291 | |
| 292 | **_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**: |
| 293 | This macro is used to re-enable the `random_shuffle` algorithm. |
| 294 | |
| 295 | **_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**: |
| 296 | This macro is used to re-enable `set_unexpected`, `get_unexpected`, and |
| 297 | `unexpected`. |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 298 | |
Mark de Wever | 0c9fe4c | 2022-07-07 19:07:03 +0200 | [diff] [blame] | 299 | C++20 Specific Configuration Macros |
| 300 | ----------------------------------- |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 301 | **_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**: |
| 302 | This macro can be used to disable diagnostics emitted from functions marked |
| 303 | ``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` |
| 304 | for more information. |
| 305 | |
Arthur O'Dwyer | a6b5107 | 2021-05-24 18:36:17 -0400 | [diff] [blame] | 306 | **_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**: |
| 307 | This macro is used to re-enable all the features removed in C++20. The effect |
| 308 | is equivalent to manually defining each macro listed below. |
| 309 | |
| 310 | **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**: |
| 311 | This macro is used to re-enable redundant members of `allocator<T>`, |
| 312 | including `pointer`, `reference`, `rebind`, `address`, `max_size`, |
| 313 | `construct`, `destroy`, and the two-argument overload of `allocate`. |
Arthur O'Dwyer | a6b5107 | 2021-05-24 18:36:17 -0400 | [diff] [blame] | 314 | |
Ilya Biryukov | 051e6b9 | 2022-06-15 10:55:55 +0200 | [diff] [blame] | 315 | **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**: |
| 316 | This macro is used to re-enable the library-provided specializations of |
| 317 | `allocator<void>` and `allocator<const void>`. |
| 318 | Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS` |
| 319 | to ensure that removed members of `allocator<void>` can be accessed. |
| 320 | |
Arthur O'Dwyer | f5486c8 | 2021-05-25 14:34:18 -0400 | [diff] [blame] | 321 | **_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**: |
| 322 | This macro is used to re-enable the `argument_type`, `result_type`, |
| 323 | `first_argument_type`, and `second_argument_type` members of class |
| 324 | templates such as `plus`, `logical_not`, `hash`, and `owner_less`. |
| 325 | |
Arthur O'Dwyer | a6b5107 | 2021-05-24 18:36:17 -0400 | [diff] [blame] | 326 | **_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**: |
| 327 | This macro is used to re-enable `not1`, `not2`, `unary_negate`, |
| 328 | and `binary_negate`. |
| 329 | |
| 330 | **_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**: |
| 331 | This macro is used to re-enable `raw_storage_iterator`. |
| 332 | |
wmbat | f5b3376 | 2021-07-02 17:08:36 +0000 | [diff] [blame] | 333 | **_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**: |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 334 | This macro is used to re-enable `is_literal_type`, `is_literal_type_v`, |
wmbat | f5b3376 | 2021-07-02 17:08:36 +0000 | [diff] [blame] | 335 | `result_of` and `result_of_t`. |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 336 | |
Louis Dionne | 4b1b70d | 2021-07-06 10:39:01 -0400 | [diff] [blame] | 337 | |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 338 | Libc++ Extensions |
| 339 | ================= |
| 340 | |
| 341 | This section documents various extensions provided by libc++, how they're |
| 342 | provided, and any information regarding how to use them. |
| 343 | |
| 344 | .. _nodiscard extension: |
| 345 | |
| 346 | Extended applications of ``[[nodiscard]]`` |
| 347 | ------------------------------------------ |
| 348 | |
| 349 | The ``[[nodiscard]]`` attribute is intended to help users find bugs where |
| 350 | function return values are ignored when they shouldn't be. After C++17 the |
| 351 | C++ standard has started to declared such library functions as ``[[nodiscard]]``. |
| 352 | However, this application is limited and applies only to dialects after C++17. |
| 353 | Users who want help diagnosing misuses of STL functions may desire a more |
| 354 | liberal application of ``[[nodiscard]]``. |
| 355 | |
| 356 | For this reason libc++ provides an extension that does just that! The |
| 357 | extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended |
| 358 | applications of ``[[nodiscard]]`` takes two forms: |
| 359 | |
| 360 | 1. Backporting ``[[nodiscard]]`` to entities declared as such by the |
| 361 | standard in newer dialects, but not in the present one. |
| 362 | |
Arthur O'Dwyer | 108facb | 2021-04-05 14:56:03 -0400 | [diff] [blame] | 363 | 2. Extended applications of ``[[nodiscard]]``, at the library's discretion, |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 364 | applied to entities never declared as such by the standard. |
| 365 | |
| 366 | Users may also opt-out of additional applications ``[[nodiscard]]`` using |
| 367 | additional macros. |
| 368 | |
| 369 | Applications of the first form, which backport ``[[nodiscard]]`` from a newer |
Arthur O'Dwyer | 108facb | 2021-04-05 14:56:03 -0400 | [diff] [blame] | 370 | dialect, may be disabled using macros specific to the dialect in which it was |
| 371 | added. For example, ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``. |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 372 | |
| 373 | Applications of the second form, which are pure extensions, may be disabled |
| 374 | by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``. |
| 375 | |
| 376 | |
| 377 | Entities declared with ``_LIBCPP_NODISCARD_EXT`` |
| 378 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 379 | |
| 380 | This section lists all extended applications of ``[[nodiscard]]`` to entities |
| 381 | which no dialect declares as such (See the second form described above). |
| 382 | |
Nico Weber | 471b10a | 2019-04-03 18:13:08 +0000 | [diff] [blame] | 383 | * ``adjacent_find`` |
| 384 | * ``all_of`` |
| 385 | * ``any_of`` |
| 386 | * ``binary_search`` |
| 387 | * ``clamp`` |
| 388 | * ``count_if`` |
| 389 | * ``count`` |
| 390 | * ``equal_range`` |
| 391 | * ``equal`` |
| 392 | * ``find_end`` |
| 393 | * ``find_first_of`` |
| 394 | * ``find_if_not`` |
| 395 | * ``find_if`` |
| 396 | * ``find`` |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 397 | * ``get_temporary_buffer`` |
Nico Weber | 471b10a | 2019-04-03 18:13:08 +0000 | [diff] [blame] | 398 | * ``includes`` |
| 399 | * ``is_heap_until`` |
| 400 | * ``is_heap`` |
| 401 | * ``is_partitioned`` |
| 402 | * ``is_permutation`` |
| 403 | * ``is_sorted_until`` |
| 404 | * ``is_sorted`` |
| 405 | * ``lexicographical_compare`` |
| 406 | * ``lower_bound`` |
| 407 | * ``max_element`` |
| 408 | * ``max`` |
| 409 | * ``min_element`` |
| 410 | * ``min`` |
| 411 | * ``minmax_element`` |
| 412 | * ``minmax`` |
| 413 | * ``mismatch`` |
| 414 | * ``none_of`` |
| 415 | * ``remove_if`` |
| 416 | * ``remove`` |
| 417 | * ``search_n`` |
| 418 | * ``search`` |
| 419 | * ``unique`` |
| 420 | * ``upper_bound`` |
Louis Dionne | 346587e | 2019-08-13 11:12:28 +0000 | [diff] [blame] | 421 | * ``lock_guard``'s constructors |
Arthur O'Dwyer | 108facb | 2021-04-05 14:56:03 -0400 | [diff] [blame] | 422 | * ``as_const`` |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 423 | * ``bit_cast`` |
Arthur O'Dwyer | 108facb | 2021-04-05 14:56:03 -0400 | [diff] [blame] | 424 | * ``forward`` |
| 425 | * ``move`` |
| 426 | * ``move_if_noexcept`` |
| 427 | * ``identity::operator()`` |
| 428 | * ``to_integer`` |
| 429 | * ``to_underlying`` |
Louis Dionne | 3994846 | 2022-06-01 15:25:14 -0400 | [diff] [blame^] | 430 | |
| 431 | Additional types supported in random distributions |
| 432 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 433 | |
| 434 | The `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number |
| 435 | distributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is |
| 436 | undefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``, |
| 437 | ``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution`` |
| 438 | with ``int8_t``, ``__int128_t`` and their unsigned versions. |