C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 29 Jun 2023 15:46:29 +0100
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.

Received on 2023-06-29 14:46:41