[libcxx-commits] [libcxx] [libc++][RFC] Document when to apply [[nodiscard]] (PR #84000)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 12 17:54:54 PDT 2024
================
@@ -0,0 +1,34 @@
+======================================
+``[[nodiscard]]`` extensions in libc++
+======================================
+
+Libc++ adds ``[[nodiscard]]`` to functions in a lot more places than the
+standard does. Any applications of ``[[nodiscard]]`` that aren't required by the
+standard written as ``_LIBCPP_NODISCARD_EXT`` to make it possible to disable
+them. This can be done by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
+
+When should ``[[nodiscard]]`` be added to functions?
+====================================================
+
----------------
philnik777 wrote:
This would fall under "where discarding the value is most likely a misuse of the function". IMO we should diagnose these kinds of cases. In your example `any_of`, or if you want to keep identical semantics `for_each`, would be the correct algorithm to use. I think we want to drive people towards writing better code, and this helps. Also, although it's not not _super_ relevant in this case, the side effect some program is relying on may be the result of some library-internal decision that could change as the library evolves. Avoiding people writing code like that is both in their interest and ours.
https://github.com/llvm/llvm-project/pull/84000
More information about the libcxx-commits
mailing list