Never cast to (void) to ignore a [[nodiscard]] return value. If you deliberately want to discard such a result, first think hard about whether that is really a good idea (there is usually a good reason the author of the function or of the return type used [[nodiscard]] in the first place). If you still think it’s appropriate and your code reviewer agrees, use std::ignore = to turn off the warning which is simple, portable, and easy to grep.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACWgwAbeENmMkiH2dq1NAWwHJNwTx8d9rj9CK2pzmQwqvq8tVg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACWgwAbc19tFAFf4tS1hrQRsaHCmrB5DtD0KEsaT%2BBmkG4af8A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACWgwAZ9CFB6drjyx_y9tQYXBoYSyEtKxwxxVxMQ6u_7xYUD%3DA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-7jzkYNEibiFuTsdfSTY-8n%2BJrxyS71bksz7XxV0q25aw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiGgTGVyeNdKPZ5U2fX0C%3DnFNnxVDb-Uqv4ffSvcA44%2Bdw%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAw44vPTNN94hT_Q0sbUEcf_RgsheJaZyuZy94oCA1zzQ%40mail.gmail.com.
The only point I would take issue with above is that base::ignore_result() is well-known, even within Chromium.Instances of [^(](void)\w: 249 (estimated)Instances of "ignore_result(": 29 (estimated)Just for kicks, "std::ignore =": 286 (estimated)
On grep-ability: There are other uses of the string (void), which I tried to exclude as much as possible, but there are still a lot of "suppress unused" uses.Anecdotally, I've often heard individuals express surprise on hearing about ignore_result() for the first time; ignoring results isn't something one commonly wants to do (good), and when you do hit that compiler diagnostic, you're not going to be pointed at it by the documentation for the diagnostic. Discoverability is very low, and I don't see that changing, absent patching Clang.Usually when arguments with no clear "best" option like this come up internally, we turn to Code Search to get some data on what developers are actually doing already. The most popular option seems to be std::ignore or (void) by 10x.The potential compile cost of <tuple> is something to weigh carefully, but the incremental cost may not be that high with a realistic set of other includes. I suspect you're also unlikely to use std::ignore in a header, so hopefully that cost doesn't propagate beyond single .cc files. I guess if we're really worried about it, we could define some sort of base::ignore, so at least it would just be the same idiom spelled differently.--On Tue, Jan 11, 2022 at 4:59 PM Roland McGrath <mcgr...@chromium.org> wrote:The biggest advantage of `std::ignore` per se is that it's standard and so in theory standard C++ things are more familiar to the average or future C++ coder. IMHO if `std::ignore` had a no-op `operator()` as well as `operator=`, then standard familiarity would be enough reason to choose it over other alternatives. The `std::ignore = ...;` usage is certainly well-specified and is the only quasi-obvious thing the standard offers for that (if cast to void is considered "not obvious", since it's far from innately obvious and only seems so to those with long C experience). https://en.cppreference.com/w/cpp/utility/tuple/ignore does say, "It can also be used to avoid warnings from unused return values of [[nodiscard]] functions." But I don't know if it's widely known for that purpose and not just the `std::tie` purpose.Nico has pointed out some practical downsides to `std::ignore` being in <tuple> wrt compilation speed, as well as the general oddity of that, which others have noted.So I suspect that the solution that has the most consensus is `ignore_result` as it is, though myself I would take `std::ignore = ...` just to get rid of more local idioms and local support for them.On Tue, Jan 11, 2022 at 3:46 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:I strongly dislike (void), and would prefer anything over it. :)--It is cryptic and I don't believe it looks any less weird in an objective sense than "std::ignore", which has the benefit of saying exactly what it does.I vote for std::ignore first and "do nothing" second.PK
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAw44vPTNN94hT_Q0sbUEcf_RgsheJaZyuZy94oCA1zzQ%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-5JMA%2BDEdSrHq57fHMmQZ0rO2E7N0tHG%2Bke%2B6R14hUkAg%40mail.gmail.com.
On Wed, Jan 12, 2022 at 11:00 AM K. Moon <km...@chromium.org> wrote:The only point I would take issue with above is that base::ignore_result() is well-known, even within Chromium.Instances of [^(](void)\w: 249 (estimated)Instances of "ignore_result(": 29 (estimated)Just for kicks, "std::ignore =": 286 (estimated)This may not be a meaningful comparison as a lot of ignore_results have already been rewritten to std::ignore:
On Wed, Jan 12, 2022 at 8:05 AM David Benjamin <davi...@chromium.org> wrote:On Wed, Jan 12, 2022 at 11:00 AM K. Moon <km...@chromium.org> wrote:The only point I would take issue with above is that base::ignore_result() is well-known, even within Chromium.Instances of [^(](void)\w: 249 (estimated)Instances of "ignore_result(": 29 (estimated)Just for kicks, "std::ignore =": 286 (estimated)This may not be a meaningful comparison as a lot of ignore_results have already been rewritten to std::ignore:I appreciate the caveat, as the results did seem a bit strange to me. Although even then, ignore_result() would be closer to parity than clearly far ahead.
On Wed, Jan 12, 2022 at 11:11 AM K. Moon <km...@chromium.org> wrote:On Wed, Jan 12, 2022 at 8:05 AM David Benjamin <davi...@chromium.org> wrote:On Wed, Jan 12, 2022 at 11:00 AM K. Moon <km...@chromium.org> wrote:The only point I would take issue with above is that base::ignore_result() is well-known, even within Chromium.Instances of [^(](void)\w: 249 (estimated)Instances of "ignore_result(": 29 (estimated)Just for kicks, "std::ignore =": 286 (estimated)This may not be a meaningful comparison as a lot of ignore_results have already been rewritten to std::ignore:I appreciate the caveat, as the results did seem a bit strange to me. Although even then, ignore_result() would be closer to parity than clearly far ahead.I think it's just that the rewrite was mostly done. :-) My checkout happens to be before the first of those comments and...
$ git grep 'std::ignore = ' 073b3397af4ac6ae1f48cd5a90b492071e8d0525 | wc -l
18
$ git grep 'ignore_result(' 073b3397af4ac6ae1f48cd5a90b492071e8d0525 | wc -l
636(The sums don't match up because code search numbers by file and this counts by line.)
On Wed, Jan 12, 2022 at 8:05 AM David Benjamin <davi...@chromium.org> wrote:On Wed, Jan 12, 2022 at 11:00 AM K. Moon <km...@chromium.org> wrote:The only point I would take issue with above is that base::ignore_result() is well-known, even within Chromium.Instances of [^(](void)\w: 249 (estimated)Instances of "ignore_result(": 29 (estimated)Just for kicks, "std::ignore =": 286 (estimated)This may not be a meaningful comparison as a lot of ignore_results have already been rewritten to std::ignore:I appreciate the caveat, as the results did seem a bit strange to me. Although even then, ignore_result() would be closer to parity than clearly far ahead.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-4XAgC9E5WTyPUyMG-rkQ7VL3Uj1_boOJ_3asoG9%2B5e4A%40mail.gmail.com.
I ran some queries using git grep -E at 2d70c8faf9d179a6219d0962df522daf32650794 (e0582ecaa5465f52bb8ece183987a5820dd19523^), removing third_party folders that weren't third_party/blink, and removing uses that were in comments.\(void\)[[:graph:]]*\w\(: 51 lines (after manually removing some instances that were not function calls)std::ignore =: 18 linesignore_result\(: 618 linesSo base::ignore_result() was indeed the most popular by far in chromium/src before crbug.com/1285085; (void) was slightly more popular than std::ignore =, but the numbers are pretty small regardless.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaRptWktk%3DTosnSTpSKM_Li_kn0VCZ0uxpptW0V-4C3_Fw%40mail.gmail.com.
#include <tuple>
int DoSomething();
int main() {
std::ignore = DoSomething();
return 0;
}
#include <string>
#include <tuple>
int DoSomething(std::string& result);
int main() {
std::string result;
std::ignore = DoSomething(result);
return result.size();
}