Date: Fri, 6 Oct 2023 15:45:41 +0200
On 06/10/2023 11:30, Ferhat Geçdoğan via Std-Proposals wrote:
> Is there any useful use-case that makes [[discard]] unique? That's just
> a way of adding one more unuseful C++ keyword to the list in my opinion.
It's not a new keyword, it's an attribute.
Yes, there's a set of qualities that make it unique:
* It does not abuse the cast notation and the cast semantics towards
void in order to suppress a warning. Doing a cast to void (à la
`(void)f();`) isn't done because one wants a specific type conversion,
it's done to silence the [[nodiscard]] warning. There is absolutely no
language reason as of why that cast should suppress any warning -- it's
just the "escape hatch" that compiler vendors converged onto, and then
got enshrined in [[nodiscard]]'s semantics, standardizing the existing
practice.
* Its spelling is intuitive, and complements [[nodiscard]].
* **It can have a reason**, encoded in the attribute. When you read code
written 15 years earlier, you shouldn't have to second-guess "why is
this the result of this call explicitly discarded? can't this function
fail? is it because with this specific set of arguments I know for sure
it doesn't fail? or maybe it can fail but I can't handle its errors
here?" and so on.
In a C++-future project wouldn't it be nice™ to be able to have as
coding policies 1) to forbid C-style casts (and especially those to
void), and 2) require people to use [[discard("with reason")]] instead¹?
To me it's a strictly positive delta in code quality and that's why I'm
proposing it.
¹ and similarly [[maybe_unused]], instead of the same cast to void, to
suppress the "unused variable `x' in function `f' warning".
> Is there any useful use-case that makes [[discard]] unique? That's just
> a way of adding one more unuseful C++ keyword to the list in my opinion.
It's not a new keyword, it's an attribute.
Yes, there's a set of qualities that make it unique:
* It does not abuse the cast notation and the cast semantics towards
void in order to suppress a warning. Doing a cast to void (à la
`(void)f();`) isn't done because one wants a specific type conversion,
it's done to silence the [[nodiscard]] warning. There is absolutely no
language reason as of why that cast should suppress any warning -- it's
just the "escape hatch" that compiler vendors converged onto, and then
got enshrined in [[nodiscard]]'s semantics, standardizing the existing
practice.
* Its spelling is intuitive, and complements [[nodiscard]].
* **It can have a reason**, encoded in the attribute. When you read code
written 15 years earlier, you shouldn't have to second-guess "why is
this the result of this call explicitly discarded? can't this function
fail? is it because with this specific set of arguments I know for sure
it doesn't fail? or maybe it can fail but I can't handle its errors
here?" and so on.
In a C++-future project wouldn't it be nice™ to be able to have as
coding policies 1) to forbid C-style casts (and especially those to
void), and 2) require people to use [[discard("with reason")]] instead¹?
To me it's a strictly positive delta in code quality and that's why I'm
proposing it.
¹ and similarly [[maybe_unused]], instead of the same cast to void, to
suppress the "unused variable `x' in function `f' warning".
-- Giuseppe D'Angelo
Received on 2023-10-06 13:45:44