C++ Logo


Advanced search

Re: [std-proposals] Dummy names for dummy objects

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 29 Jun 2023 12:20:43 -0400
On Thu, Jun 29, 2023 at 12:01 PM Harald Achitz via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> > Use a cast to (void) to suppress nodiscardness.
> Will not pass a code review, for reason
> [[maybe_unused]] deserves a comment, for reasons, to pass a code review.
> If auto _ silences nodiscard warnings, I can guarantee you, it will land on the list of features not to use in some projects, and it will be a point of discussion if / when we move to new standard version.

Your project's proclivities are for you to decide, but you shouldn't
force others to adopt them. If `(void)` can silence such warnings, so
too should `auto _`.

> This can / should not be the indent of that actually very nice feature/idea.

This really is a problem of intent, what `nodiscard` means to you vs. others.

You seem to want "nodiscard" to be literal; that it is *linguistically
impossible* to discard the value. Except... the language doesn't
prevent that:

auto var = foo(); //var is never used

That discards the value just as sure as `auto _` would. And this code
gives exactly zero compile errors. `nodiscard` could have been defined
such that it would fail in this instance, but it isn't. This is 100%
legal C++ code.

Now, I'm sure you have warnings turned on for unused variables, but
those would warn even if `foo` returned something that *wasn't*
`nodiscard`. So that warning isn't really about whether the value is
being discarded when it is marked `nodiscard`.

The point of `nodiscard` is not to make it *impossible* to discard a
value. It is to make it impossible to *accidentally* discard a value.
Typing `auto _ =` or `(void)` to discard such a value is a deliberate
act; it's not something you can do by accident. Therefore, the feature
is still doing its job even with `auto _`.

We just have a disagreement about what that job is.

Received on 2023-06-29 16:20:53