C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Similar to [[no_discard]], but store in a variable, maybe call it [[must_store]]

From: Jarrad Waterloo <descender76_at_[hidden]>
Date: Tue, 22 Jul 2025 08:58:58 -0400
What about the opposite [[must_not_store]] /
[[must_not_store_in_locally_named_variables]]?

return *this;// is meant for function chaining i.e. temporary use not
storing in local variables which results in more aliases and increased
chances of invalidation dangling
This might also benefit the proper use of range based views.
Ultimately this is about alias reduction. Those that support unique
ownership pointers, i.e. borrow pointers, might be empathise with this one.



On Tue, Jul 22, 2025 at 8:38 AM Jan Schultke via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Another way to use the c_str() member function you've mentioned,
> without involving a variable would be:
>
> > void f(const char*);
> > wxString mystr( wxS("Hello I'm a monkey") );
> > f(mystr.c_str());
>
> So it's not actually a function that requires [[must_store]] for its
> result. Similarly, I can imagine that you could bind an
> rvalue/forwarding reference to the result of wlock() when passing that
> into a function or so.
>
> All that is to say: it's hard to put your finger on what
> [[must_store]] actually means. If it means that you can neither
> discard the result, nor create a temporary object from it, then
> neither of your examples seem like obviously valid use cases. Does
> there even exist a valid example?
>
> There's also a question of how to suppress a warning produced by
> [[must_store]]. [[nodiscard]] can be easily suppressed by casting to
> void, but that [[must_store]] could presumably not be suppressed in
> this way, since casting to void is obviously not storing. However,
> storing in a variable for the purpose of suppression would always
> involve prolonging the lifetime of an otherwise temporary object, and
> it feels wrong if you cannot suppress that warning without such
> semantic impact.
>
> Anyhow, I think the costs outweigh the benefits here. [[nodiscard]]
> has much better cost/benefit.
>
>
> Jan
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-07-22 12:59:15