C++ Logo


Advanced search

Re: [std-proposals] New function attribute [[nodiscard_scope]]

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 29 Jun 2023 17:37:08 -0400
On Thu, Jun 29, 2023 at 5:24 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Thu, Jun 29, 2023 at 8:41 PM Andrew Tomazos <andrewtomazos_at_[hidden]> wrote:
> >
> > What's an example of a realistic bug that [[nodiscard_scope]] would catch but [[nodiscard]] wouldn't?
> synchronized_value<MyClass> g_obj;
> void Func(void)
> {
> auto &myref = *g_obj.synchronize(); // This mistake would be
> caught by [[nodiscard_scope]]
> myref.SomeMethod();
> }

Not according to your definition of the functionality:

> an object that's intended to become a local object

The temporary in question did in fact "become a local object". A
temporary, in fact. So by your definition, it should not be a problem.

Perhaps you meant local *variable*. In which case, what happens if you
use it to initialize a function parameter:

some_func(g_obj.synchronize()); //Should only synchronize for the
duration of the function call.

Is this something users should be able to do, or is it something that
should be forbidden? It isn't creating a local variable; it's
initializing a function parameter. And if it should be forbidden...
why? That sounds like a pretty reasonable thing to want to do: to call
a function within an object's lock.

Received on 2023-06-29 21:37:19