[libcxx-commits] [libcxx] [libc++][RFC] Document when to apply [[nodiscard]] (PR #84000)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 12 17:39:25 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?
+====================================================
+
----------------
EricWF wrote:
What about things that potentially have side effects, and could be used (or mis-used) in meaningful ways?
Take for example
```
bool would_overflow(std::vector<int> x) {
bool overflows = false;
std::accumulate(x.begin(), x.end(), 0,
[&] (auto lhs, auto rhs) {
if () {
overflows = true;
return INT_MAX
}
return lhs + rhs;
});
return overflows;
}
```
The codes a bit contrived, but I've seen a lot of similar things in the wild. The pattern is always the same, they sidecar information in the binary operator and then return that sidecar-ed information rather than the primary result.
Where do we stand on cases like these? They're semantically meaningfully, most often intentional, but perhaps poor style.
https://github.com/llvm/llvm-project/pull/84000
More information about the libcxx-commits
mailing list