xref: /openbsd-src/gnu/llvm/libcxx/docs/UsingLibcxx.rst (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
176d0caaeSpatrick.. _using-libcxx:
276d0caaeSpatrick
346035553Spatrick============
446035553SpatrickUsing libc++
546035553Spatrick============
646035553Spatrick
746035553Spatrick.. contents::
846035553Spatrick  :local:
946035553Spatrick
1076d0caaeSpatrickUsually, libc++ is packaged and shipped by a vendor through some delivery vehicle
1176d0caaeSpatrick(operating system distribution, SDK, toolchain, etc) and users don't need to do
1276d0caaeSpatrickanything special in order to use the library.
1346035553Spatrick
1476d0caaeSpatrickThis page contains information about configuration knobs that can be used by
1576d0caaeSpatrickusers when they know libc++ is used by their toolchain, and how to use libc++
1676d0caaeSpatrickwhen it is not the default library used by their toolchain.
1776d0caaeSpatrick
1876d0caaeSpatrick
1976d0caaeSpatrickUsing a different version of the C++ Standard
2076d0caaeSpatrick=============================================
2176d0caaeSpatrick
2276d0caaeSpatrickLibc++ implements the various versions of the C++ Standard. Changing the version of
2376d0caaeSpatrickthe standard can be done by passing ``-std=c++XY`` to the compiler. Libc++ will
2476d0caaeSpatrickautomatically detect what Standard is being used and will provide functionality that
2576d0caaeSpatrickmatches that Standard in the library.
2646035553Spatrick
2746035553Spatrick.. code-block:: bash
2846035553Spatrick
2976d0caaeSpatrick  $ clang++ -std=c++17 test.cpp
3046035553Spatrick
3176d0caaeSpatrick.. warning::
3276d0caaeSpatrick  Using ``-std=c++XY`` with a version of the Standard that has not been ratified yet
3376d0caaeSpatrick  is considered unstable. Libc++ reserves the right to make breaking changes to the
3476d0caaeSpatrick  library until the standard has been ratified.
3546035553Spatrick
3646035553Spatrick
37*4bdff4beSrobertEnabling experimental C++ Library features
38*4bdff4beSrobert==========================================
3946035553Spatrick
40*4bdff4beSrobertLibc++ provides implementations of some experimental features. Experimental features
41*4bdff4beSrobertare either Technical Specifications (TSes) or official features that were voted to
42*4bdff4beSrobertthe Standard but whose implementation is not complete or stable yet in libc++. Those
43*4bdff4beSrobertare disabled by default because they are neither API nor ABI stable. However, the
44*4bdff4beSrobert``-fexperimental-library`` compiler flag can be defined to turn those features on.
4546035553Spatrick
4646035553Spatrick.. warning::
47*4bdff4beSrobert  Experimental libraries are experimental.
48*4bdff4beSrobert    * The contents of the ``<experimental/...>`` headers and the associated static
4946035553Spatrick      library will not remain compatible between versions.
5046035553Spatrick    * No guarantees of API or ABI stability are provided.
5176d0caaeSpatrick    * When the standardized version of an experimental feature is implemented,
5246035553Spatrick      the experimental feature is removed two releases after the non-experimental
5346035553Spatrick      version has shipped. The full policy is explained :ref:`here <experimental features>`.
5446035553Spatrick
55*4bdff4beSrobert.. note::
56*4bdff4beSrobert  On compilers that do not support the ``-fexperimental-library`` flag, users can
57*4bdff4beSrobert  define the ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro and manually link against the
58*4bdff4beSrobert  appropriate static library (usually shipped as ``libc++experimental.a``) to get
59*4bdff4beSrobert  access to experimental library features.
60*4bdff4beSrobert
6146035553Spatrick
6276d0caaeSpatrickUsing libc++ when it is not the system default
6376d0caaeSpatrick==============================================
6476d0caaeSpatrick
6576d0caaeSpatrickOn systems where libc++ is provided but is not the default, Clang provides a flag
6676d0caaeSpatrickcalled ``-stdlib=`` that can be used to decide which standard library is used.
6776d0caaeSpatrickUsing ``-stdlib=libc++`` will select libc++:
6846035553Spatrick
6946035553Spatrick.. code-block:: bash
7046035553Spatrick
7176d0caaeSpatrick  $ clang++ -stdlib=libc++ test.cpp
7246035553Spatrick
7376d0caaeSpatrickOn systems where libc++ is the library in use by default such as macOS and FreeBSD,
7476d0caaeSpatrickthis flag is not required.
7576d0caaeSpatrick
7676d0caaeSpatrick
7776d0caaeSpatrick.. _alternate libcxx:
7876d0caaeSpatrick
7976d0caaeSpatrickUsing a custom built libc++
8076d0caaeSpatrick===========================
8176d0caaeSpatrick
8276d0caaeSpatrickMost compilers provide a way to disable the default behavior for finding the
8376d0caaeSpatrickstandard library and to override it with custom paths. With Clang, this can
8476d0caaeSpatrickbe done with:
8546035553Spatrick
8646035553Spatrick.. code-block:: bash
8746035553Spatrick
8876d0caaeSpatrick  $ clang++ -nostdinc++ -nostdlib++           \
8976d0caaeSpatrick            -isystem <install>/include/c++/v1 \
9076d0caaeSpatrick            -L <install>/lib                  \
9176d0caaeSpatrick            -Wl,-rpath,<install>/lib          \
9276d0caaeSpatrick            -lc++                             \
9376d0caaeSpatrick            test.cpp
9446035553Spatrick
9576d0caaeSpatrickThe option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path,
9676d0caaeSpatrickwhich causes the system's dynamic linker to look for libc++ in ``<install>/lib``
9776d0caaeSpatrickwhenever the program is loaded.
9846035553Spatrick
9976d0caaeSpatrickGCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs``
10076d0caaeSpatrickinstead. Since that removes all the standard system libraries and not just libc++,
10176d0caaeSpatrickthe system libraries must be re-added manually. For example:
10246035553Spatrick
10346035553Spatrick.. code-block:: bash
10446035553Spatrick
10576d0caaeSpatrick  $ g++ -nostdinc++ -nodefaultlibs           \
10676d0caaeSpatrick        -isystem <install>/include/c++/v1    \
10776d0caaeSpatrick        -L <install>/lib                     \
10876d0caaeSpatrick        -Wl,-rpath,<install>/lib             \
10976d0caaeSpatrick        -lc++ -lc++abi -lm -lc -lgcc_s -lgcc \
11076d0caaeSpatrick        test.cpp
11146035553Spatrick
11246035553Spatrick
11346035553SpatrickGDB Pretty printers for libc++
11476d0caaeSpatrick==============================
11546035553Spatrick
11676d0caaeSpatrickGDB does not support pretty-printing of libc++ symbols by default. However, libc++ does
11776d0caaeSpatrickprovide pretty-printers itself. Those can be used as:
11846035553Spatrick
11976d0caaeSpatrick.. code-block:: bash
12046035553Spatrick
12176d0caaeSpatrick  $ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \
12276d0caaeSpatrick        -ex "python register_libcxx_printer_loader()" \
12376d0caaeSpatrick        <args>
12446035553Spatrick
125*4bdff4beSrobert.. _include-what-you-use:
126*4bdff4beSrobert
127*4bdff4beSrobertinclude-what-you-use (IWYU)
128*4bdff4beSrobert===========================
129*4bdff4beSrobert
130*4bdff4beSrobertlibc++ provides an IWYU `mapping file <https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUMappings.md>`,
131*4bdff4beSrobertwhich drastically improves the accuracy of the tool when using libc++. To use the mapping file with
132*4bdff4beSrobertIWYU, you should run the tool like so:
133*4bdff4beSrobert
134*4bdff4beSrobert.. code-block:: bash
135*4bdff4beSrobert
136*4bdff4beSrobert  $ include-what-you-use -Xiwyu /path/to/libcxx/include/libcxx.imp file.cpp
137*4bdff4beSrobert
138*4bdff4beSrobertIf you would prefer to not use that flag, then you can replace ``/path/to/include-what-you-use/share/libcxx.imp```
139*4bdff4beSrobertfile with the libc++-provided ``libcxx.imp`` file.
140*4bdff4beSrobert
141*4bdff4beSrobert.. _assertions-mode:
142*4bdff4beSrobert
143*4bdff4beSrobertEnabling the "safe libc++" mode
144*4bdff4beSrobert===============================
145*4bdff4beSrobert
146*4bdff4beSrobertLibc++ contains a number of assertions whose goal is to catch undefined behavior in the
147*4bdff4beSrobertlibrary, usually caused by precondition violations. Those assertions do not aim to be
148*4bdff4beSrobertexhaustive -- instead they aim to provide a good balance between safety and performance.
149*4bdff4beSrobertIn particular, these assertions do not change the complexity of algorithms. However, they
150*4bdff4beSrobertmight, in some cases, interfere with compiler optimizations.
151*4bdff4beSrobert
152*4bdff4beSrobertBy default, these assertions are turned off. Vendors can decide to turn them on while building
153*4bdff4beSrobertthe compiled library by defining ``LIBCXX_ENABLE_ASSERTIONS=ON`` at CMake configuration time.
154*4bdff4beSrobertWhen ``LIBCXX_ENABLE_ASSERTIONS`` is used, the compiled library will be built with assertions
155*4bdff4beSrobertenabled, **and** user code will be built with assertions enabled by default. If
156*4bdff4beSrobert``LIBCXX_ENABLE_ASSERTIONS=OFF`` at CMake configure time, the compiled library will not contain
157*4bdff4beSrobertassertions and the default when building user code will be to have assertions disabled.
158*4bdff4beSrobertAs a user, you can consult your vendor to know whether assertions are enabled by default.
159*4bdff4beSrobert
160*4bdff4beSrobertFurthermore, independently of any vendor-selected default, users can always control whether
161*4bdff4beSrobertassertions are enabled in their code by defining ``_LIBCPP_ENABLE_ASSERTIONS=0|1`` before
162*4bdff4beSrobertincluding any libc++ header (we recommend passing ``-D_LIBCPP_ENABLE_ASSERTIONS=X`` to the
163*4bdff4beSrobertcompiler). Note that if the compiled library was built by the vendor without assertions,
164*4bdff4beSrobertfunctions compiled inside the static or shared library won't have assertions enabled even
165*4bdff4beSrobertif the user defines ``_LIBCPP_ENABLE_ASSERTIONS=1`` (the same is true for the inverse case
166*4bdff4beSrobertwhere the static or shared library was compiled **with** assertions but the user tries to
167*4bdff4beSrobertdisable them). However, most of the code in libc++ is in the headers, so the user-selected
168*4bdff4beSrobertvalue for ``_LIBCPP_ENABLE_ASSERTIONS`` (if any) will usually be respected.
169*4bdff4beSrobert
170*4bdff4beSrobertWhen an assertion fails, the program is aborted through a special verbose termination function. The
171*4bdff4beSrobertlibrary provides a default function that prints an error message and calls ``std::abort()``. Note
172*4bdff4beSrobertthat this function is provided by the static or shared library, so it is only available when deploying
173*4bdff4beSrobertto a platform where the compiled library is sufficiently recent. On older platforms, the program will
174*4bdff4beSrobertterminate in an unspecified unsuccessful manner, but the quality of diagnostics won't be great.
175*4bdff4beSrobertHowever, users can also override that mechanism at two different levels. First, the mechanism can be
176*4bdff4beSrobertoverriden at compile-time by defining the ``_LIBCPP_VERBOSE_ABORT(format, args...)`` variadic macro.
177*4bdff4beSrobertWhen that macro is defined, it will be called with a format string as the first argument, followed by
178*4bdff4beSroberta series of arguments to format using printf-style formatting. Compile-time customization may be
179*4bdff4beSrobertinteresting to get precise control over code generation, however it is also inconvenient to use in
180*4bdff4beSrobertsome cases. Indeed, compile-time customization of the verbose termination function requires that all
181*4bdff4beSroberttranslation units be compiled with a consistent definition for ``_LIBCPP_VERBOSE_ABORT`` to avoid ODR
182*4bdff4beSrobertviolations, which can add complexity in the build system of users.
183*4bdff4beSrobert
184*4bdff4beSrobertOtherwise, if compile-time customization is not necessary, link-time customization of the handler is also
185*4bdff4beSrobertpossible, similarly to how replacing ``operator new`` works. This mechanism trades off fine-grained control
186*4bdff4beSrobertover the call site where the termination is initiated in exchange for more ergonomics. Link-time customization
187*4bdff4beSrobertis done by simply defining the following function in exactly one translation unit of your program:
188*4bdff4beSrobert
189*4bdff4beSrobert.. code-block:: cpp
190*4bdff4beSrobert
191*4bdff4beSrobert  void __libcpp_verbose_abort(char const* format, ...)
192*4bdff4beSrobert
193*4bdff4beSrobertThis mechanism is similar to how one can replace the default definition of ``operator new``
194*4bdff4beSrobertand ``operator delete``. For example:
195*4bdff4beSrobert
196*4bdff4beSrobert.. code-block:: cpp
197*4bdff4beSrobert
198*4bdff4beSrobert  // In HelloWorldHandler.cpp
199*4bdff4beSrobert  #include <version> // must include any libc++ header before defining the function (C compatibility headers excluded)
200*4bdff4beSrobert
201*4bdff4beSrobert  void std::__libcpp_verbose_abort(char const* format, ...) {
202*4bdff4beSrobert    va_list list;
203*4bdff4beSrobert    va_start(list, format);
204*4bdff4beSrobert    std::vfprintf(stderr, format, list);
205*4bdff4beSrobert    va_end(list);
206*4bdff4beSrobert
207*4bdff4beSrobert    std::abort();
208*4bdff4beSrobert  }
209*4bdff4beSrobert
210*4bdff4beSrobert  // In HelloWorld.cpp
211*4bdff4beSrobert  #include <vector>
212*4bdff4beSrobert
213*4bdff4beSrobert  int main() {
214*4bdff4beSrobert    std::vector<int> v;
215*4bdff4beSrobert    int& x = v[0]; // Your termination function will be called here if _LIBCPP_ENABLE_ASSERTIONS=1
216*4bdff4beSrobert  }
217*4bdff4beSrobert
218*4bdff4beSrobertAlso note that the verbose termination function should never return. Since assertions in libc++
219*4bdff4beSrobertcatch undefined behavior, your code will proceed with undefined behavior if your function is called
220*4bdff4beSrobertand does return.
221*4bdff4beSrobert
222*4bdff4beSrobertFurthermore, exceptions should not be thrown from the function. Indeed, many functions in the
223*4bdff4beSrobertlibrary are ``noexcept``, and any exception thrown from the termination function will result
224*4bdff4beSrobertin ``std::terminate`` being called.
22546035553Spatrick
22646035553SpatrickLibc++ Configuration Macros
22746035553Spatrick===========================
22846035553Spatrick
22946035553SpatrickLibc++ provides a number of configuration macros which can be used to enable
23046035553Spatrickor disable extended libc++ behavior, including enabling "debug mode" or
23146035553Spatrickthread safety annotations.
23246035553Spatrick
23346035553Spatrick**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
23446035553Spatrick  This macro is used to enable -Wthread-safety annotations on libc++'s
23576d0caaeSpatrick  ``std::mutex`` and ``std::lock_guard``. By default, these annotations are
23646035553Spatrick  disabled and must be manually enabled by the user.
23746035553Spatrick
23846035553Spatrick**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
23946035553Spatrick  This macro is used to disable all visibility annotations inside libc++.
24046035553Spatrick  Defining this macro and then building libc++ with hidden visibility gives a
24146035553Spatrick  build of libc++ which does not export any symbols, which can be useful when
24246035553Spatrick  building statically for inclusion into another library.
24346035553Spatrick
24446035553Spatrick**_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
24546035553Spatrick  This macro disables the additional diagnostics generated by libc++ using the
24646035553Spatrick  `diagnose_if` attribute. These additional diagnostics include checks for:
24746035553Spatrick
24846035553Spatrick    * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
24946035553Spatrick      counterparts a comparator which is not const callable.
25046035553Spatrick    * Giving an unordered associative container a hasher that is not const
25146035553Spatrick      callable.
25246035553Spatrick
25346035553Spatrick**_LIBCPP_NO_VCRUNTIME**:
25446035553Spatrick  Microsoft's C and C++ headers are fairly entangled, and some of their C++
25546035553Spatrick  headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
25646035553Spatrick  in from a lot of other headers and provides definitions which clash with
25746035553Spatrick  libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
25846035553Spatrick  there's no way for libc++ to provide a compatible definition, since you can't
25946035553Spatrick  have multiple definitions).
26046035553Spatrick
26146035553Spatrick  By default, libc++ solves this problem by deferring to Microsoft's vcruntime
26246035553Spatrick  headers where needed. However, it may be undesirable to depend on vcruntime
26346035553Spatrick  headers, since they may not always be available in cross-compilation setups,
26446035553Spatrick  or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
26546035553Spatrick  prevents libc++ from depending on vcruntime headers. Consequently, it also
26646035553Spatrick  prevents libc++ headers from being interoperable with vcruntime headers (from
26746035553Spatrick  the aforementioned clashes), so users of this macro are promising to not
26846035553Spatrick  attempt to combine libc++ headers with the problematic vcruntime headers. This
26946035553Spatrick  macro also currently prevents certain `operator new`/`operator delete`
27046035553Spatrick  replacement scenarios from working, e.g. replacing `operator new` and
27146035553Spatrick  expecting a non-replaced `operator new[]` to call the replaced `operator new`.
27246035553Spatrick
27346035553Spatrick**_LIBCPP_DISABLE_NODISCARD_EXT**:
274*4bdff4beSrobert  This macro disables library-extensions of ``[[nodiscard]]``.
275*4bdff4beSrobert  See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for more information.
27646035553Spatrick
27746035553Spatrick**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
27846035553Spatrick  This macro disables warnings when using deprecated components. For example,
27946035553Spatrick  using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
28046035553Spatrick  warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
28146035553Spatrick  no warning will be emitted. By default, this macro is not defined.
28246035553Spatrick
28346035553SpatrickC++17 Specific Configuration Macros
28446035553Spatrick-----------------------------------
28546035553Spatrick**_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
28646035553Spatrick  This macro is used to re-enable all the features removed in C++17. The effect
28746035553Spatrick  is equivalent to manually defining each macro listed below.
28846035553Spatrick
28946035553Spatrick**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
29076d0caaeSpatrick  This macro is used to re-enable `auto_ptr`.
29146035553Spatrick
29276d0caaeSpatrick**_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**:
29376d0caaeSpatrick  This macro is used to re-enable the `binder1st`, `binder2nd`,
29476d0caaeSpatrick  `pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`,
29576d0caaeSpatrick  `mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`,
29676d0caaeSpatrick  `const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t`
29776d0caaeSpatrick  class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`,
29876d0caaeSpatrick  and `ptr_fun` functions.
29976d0caaeSpatrick
30076d0caaeSpatrick**_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**:
30176d0caaeSpatrick  This macro is used to re-enable the `random_shuffle` algorithm.
30276d0caaeSpatrick
30376d0caaeSpatrick**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
30476d0caaeSpatrick  This macro is used to re-enable `set_unexpected`, `get_unexpected`, and
30576d0caaeSpatrick  `unexpected`.
30676d0caaeSpatrick
307*4bdff4beSrobertC++20 Specific Configuration Macros
308*4bdff4beSrobert-----------------------------------
30946035553Spatrick**_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
31046035553Spatrick  This macro can be used to disable diagnostics emitted from functions marked
31146035553Spatrick  ``[[nodiscard]]`` in dialects after C++17.  See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
31246035553Spatrick  for more information.
31346035553Spatrick
31476d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**:
31576d0caaeSpatrick  This macro is used to re-enable all the features removed in C++20. The effect
31676d0caaeSpatrick  is equivalent to manually defining each macro listed below.
31776d0caaeSpatrick
31876d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**:
31976d0caaeSpatrick  This macro is used to re-enable redundant members of `allocator<T>`,
32076d0caaeSpatrick  including `pointer`, `reference`, `rebind`, `address`, `max_size`,
32176d0caaeSpatrick  `construct`, `destroy`, and the two-argument overload of `allocate`.
32276d0caaeSpatrick
323*4bdff4beSrobert**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**:
324*4bdff4beSrobert  This macro is used to re-enable the library-provided specializations of
325*4bdff4beSrobert  `allocator<void>` and `allocator<const void>`.
326*4bdff4beSrobert  Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`
327*4bdff4beSrobert  to ensure that removed members of `allocator<void>` can be accessed.
328*4bdff4beSrobert
32976d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**:
33076d0caaeSpatrick  This macro is used to re-enable the `argument_type`, `result_type`,
33176d0caaeSpatrick  `first_argument_type`, and `second_argument_type` members of class
33276d0caaeSpatrick  templates such as `plus`, `logical_not`, `hash`, and `owner_less`.
33376d0caaeSpatrick
33476d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**:
33576d0caaeSpatrick  This macro is used to re-enable `not1`, `not2`, `unary_negate`,
33676d0caaeSpatrick  and `binary_negate`.
33776d0caaeSpatrick
33876d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**:
33976d0caaeSpatrick  This macro is used to re-enable `raw_storage_iterator`.
34076d0caaeSpatrick
34176d0caaeSpatrick**_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**:
34276d0caaeSpatrick  This macro is used to re-enable `is_literal_type`, `is_literal_type_v`,
34376d0caaeSpatrick  `result_of` and `result_of_t`.
34476d0caaeSpatrick
34546035553Spatrick
34646035553SpatrickLibc++ Extensions
34746035553Spatrick=================
34846035553Spatrick
34946035553SpatrickThis section documents various extensions provided by libc++, how they're
35046035553Spatrickprovided, and any information regarding how to use them.
35146035553Spatrick
35246035553Spatrick.. _nodiscard extension:
35346035553Spatrick
35446035553SpatrickExtended applications of ``[[nodiscard]]``
35546035553Spatrick------------------------------------------
35646035553Spatrick
35746035553SpatrickThe ``[[nodiscard]]`` attribute is intended to help users find bugs where
35846035553Spatrickfunction return values are ignored when they shouldn't be. After C++17 the
35946035553SpatrickC++ standard has started to declared such library functions as ``[[nodiscard]]``.
36046035553SpatrickHowever, this application is limited and applies only to dialects after C++17.
36146035553SpatrickUsers who want help diagnosing misuses of STL functions may desire a more
36246035553Spatrickliberal application of ``[[nodiscard]]``.
36346035553Spatrick
36446035553SpatrickFor this reason libc++ provides an extension that does just that! The
365*4bdff4beSrobertextension is enabled by default and can be disabled by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
366*4bdff4beSrobertThe extended applications of ``[[nodiscard]]`` takes two forms:
36746035553Spatrick
36846035553Spatrick1. Backporting ``[[nodiscard]]`` to entities declared as such by the
36946035553Spatrick   standard in newer dialects, but not in the present one.
37046035553Spatrick
37176d0caaeSpatrick2. Extended applications of ``[[nodiscard]]``, at the library's discretion,
37246035553Spatrick   applied to entities never declared as such by the standard.
37346035553Spatrick
37446035553SpatrickEntities declared with ``_LIBCPP_NODISCARD_EXT``
37546035553Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37646035553Spatrick
37746035553SpatrickThis section lists all extended applications of ``[[nodiscard]]`` to entities
37846035553Spatrickwhich no dialect declares as such (See the second form described above).
37946035553Spatrick
38046035553Spatrick* ``adjacent_find``
38146035553Spatrick* ``all_of``
38246035553Spatrick* ``any_of``
38346035553Spatrick* ``binary_search``
38446035553Spatrick* ``clamp``
38546035553Spatrick* ``count_if``
38646035553Spatrick* ``count``
38746035553Spatrick* ``equal_range``
38846035553Spatrick* ``equal``
38946035553Spatrick* ``find_end``
39046035553Spatrick* ``find_first_of``
39146035553Spatrick* ``find_if_not``
39246035553Spatrick* ``find_if``
39346035553Spatrick* ``find``
39446035553Spatrick* ``get_temporary_buffer``
39546035553Spatrick* ``includes``
39646035553Spatrick* ``is_heap_until``
39746035553Spatrick* ``is_heap``
39846035553Spatrick* ``is_partitioned``
39946035553Spatrick* ``is_permutation``
40046035553Spatrick* ``is_sorted_until``
40146035553Spatrick* ``is_sorted``
40246035553Spatrick* ``lexicographical_compare``
40346035553Spatrick* ``lower_bound``
40446035553Spatrick* ``max_element``
40546035553Spatrick* ``max``
40646035553Spatrick* ``min_element``
40746035553Spatrick* ``min``
40846035553Spatrick* ``minmax_element``
40946035553Spatrick* ``minmax``
41046035553Spatrick* ``mismatch``
41146035553Spatrick* ``none_of``
41246035553Spatrick* ``remove_if``
41346035553Spatrick* ``remove``
41446035553Spatrick* ``search_n``
41546035553Spatrick* ``search``
41646035553Spatrick* ``unique``
41746035553Spatrick* ``upper_bound``
418*4bdff4beSrobert* ``ranges::adjacent_find``
419*4bdff4beSrobert* ``ranges::all_of``
420*4bdff4beSrobert* ``ranges::any_of``
421*4bdff4beSrobert* ``ranges::binary_search``
422*4bdff4beSrobert* ``ranges::clamp``
423*4bdff4beSrobert* ``ranges::count_if``
424*4bdff4beSrobert* ``ranges::count``
425*4bdff4beSrobert* ``ranges::equal_range``
426*4bdff4beSrobert* ``ranges::equal``
427*4bdff4beSrobert* ``ranges::find_end``
428*4bdff4beSrobert* ``ranges::find_first_of``
429*4bdff4beSrobert* ``ranges::find_if_not``
430*4bdff4beSrobert* ``ranges::find_if``
431*4bdff4beSrobert* ``ranges::find``
432*4bdff4beSrobert* ``ranges::get_temporary_buffer``
433*4bdff4beSrobert* ``ranges::includes``
434*4bdff4beSrobert* ``ranges::is_heap_until``
435*4bdff4beSrobert* ``ranges::is_heap``
436*4bdff4beSrobert* ``ranges::is_partitioned``
437*4bdff4beSrobert* ``ranges::is_permutation``
438*4bdff4beSrobert* ``ranges::is_sorted_until``
439*4bdff4beSrobert* ``ranges::is_sorted``
440*4bdff4beSrobert* ``ranges::lexicographical_compare``
441*4bdff4beSrobert* ``ranges::lower_bound``
442*4bdff4beSrobert* ``ranges::max_element``
443*4bdff4beSrobert* ``ranges::max``
444*4bdff4beSrobert* ``ranges::min_element``
445*4bdff4beSrobert* ``ranges::min``
446*4bdff4beSrobert* ``ranges::minmax_element``
447*4bdff4beSrobert* ``ranges::minmax``
448*4bdff4beSrobert* ``ranges::mismatch``
449*4bdff4beSrobert* ``ranges::none_of``
450*4bdff4beSrobert* ``ranges::remove_if``
451*4bdff4beSrobert* ``ranges::remove``
452*4bdff4beSrobert* ``ranges::search_n``
453*4bdff4beSrobert* ``ranges::search``
454*4bdff4beSrobert* ``ranges::unique``
455*4bdff4beSrobert* ``ranges::upper_bound``
45646035553Spatrick* ``lock_guard``'s constructors
45776d0caaeSpatrick* ``as_const``
458*4bdff4beSrobert* ``bit_cast``
45976d0caaeSpatrick* ``forward``
46076d0caaeSpatrick* ``move``
46176d0caaeSpatrick* ``move_if_noexcept``
46276d0caaeSpatrick* ``identity::operator()``
46376d0caaeSpatrick* ``to_integer``
46476d0caaeSpatrick* ``to_underlying``
465*4bdff4beSrobert* ``signbit``
466*4bdff4beSrobert* ``fpclassify``
467*4bdff4beSrobert* ``isfinite``
468*4bdff4beSrobert* ``isinf``
469*4bdff4beSrobert* ``isnan``
470*4bdff4beSrobert* ``isnormal``
471*4bdff4beSrobert* ``isgreater``
472*4bdff4beSrobert* ``isgreaterequal``
473*4bdff4beSrobert* ``isless``
474*4bdff4beSrobert* ``islessequal``
475*4bdff4beSrobert* ``islessgreater``
476*4bdff4beSrobert* ``isunordered``
477*4bdff4beSrobert* ``ceil``
478*4bdff4beSrobert* ``fabs``
479*4bdff4beSrobert* ``floor``
480*4bdff4beSrobert* ``cbrt``
481*4bdff4beSrobert* ``copysign``
482*4bdff4beSrobert* ``fmax``
483*4bdff4beSrobert* ``fmin``
484*4bdff4beSrobert* ``nearbyint``
485*4bdff4beSrobert* ``rint``
486*4bdff4beSrobert* ``round``
487*4bdff4beSrobert* ``trunc``
488*4bdff4beSrobert
489*4bdff4beSrobertExtended integral type support
490*4bdff4beSrobert------------------------------
491*4bdff4beSrobert
492*4bdff4beSrobertSeveral platforms support types that are not specified in the Standard, such as
493*4bdff4beSrobertthe 128-bit integral types ``__int128_t`` and ``__uint128_t``. As an extension,
494*4bdff4beSrobertlibc++ does a best-effort attempt to support these types like other integral
495*4bdff4beSroberttypes, by supporting them notably in:
496*4bdff4beSrobert
497*4bdff4beSrobert* ``<bits>``
498*4bdff4beSrobert* ``<charconv>``
499*4bdff4beSrobert* ``<functional>``
500*4bdff4beSrobert* ``<type_traits>``
501*4bdff4beSrobert* ``<format>``
502*4bdff4beSrobert* ``<random>``
503*4bdff4beSrobert
504*4bdff4beSrobertAdditional types supported in random distributions
505*4bdff4beSrobert~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
506*4bdff4beSrobert
507*4bdff4beSrobertThe `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number
508*4bdff4beSrobertdistributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is
509*4bdff4beSrobertundefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``,
510*4bdff4beSrobert``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution``
511*4bdff4beSrobertwith ``int8_t``, ``__int128_t`` and their unsigned versions.
512*4bdff4beSrobert
513*4bdff4beSrobertExtensions to ``<format>``
514*4bdff4beSrobert--------------------------
515*4bdff4beSrobert
516*4bdff4beSrobertThe exposition only type ``basic-format-string`` and its typedefs
517*4bdff4beSrobert``format-string`` and ``wformat-string`` became ``basic_format_string``,
518*4bdff4beSrobert``format_string``, and ``wformat_string`` in C++23. Libc++ makes these types
519*4bdff4beSrobertavailable in C++20 as an extension.
520