C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 29 Jun 2023 12:58:36 -0400
On Thu, Jun 29, 2023 at 10:46 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> If we want a function's return value not to be discarded, we mark it
> with [[nodiscard]].
>
> In the case of 'std::synchronized_value', the method 'synchronize'
> returns an object that's intended to become a local object, as in:
>
> std::synchronized_value<MyClass> g_obj;
>
> int main(void)
> {
> auto mylock = g_obj.synchronize();
>
> mylock->SomeMethod();
> mylock->SomeOtherMethod();
> }
>
> So maybe we should have a new attribute [[nodiscard_scope]] to
> indicate that the return value should become a local object in the
> calling function.

I'm not sure I see a difference. To "become a local object" is to not
discard it. The two cases are synonyms. The point of these attributes
is to prevent mistakes. Can you provide a scenario where one attribute
finds mistaken misuse while the other does not?

Received on 2023-06-29 16:58:47