C++ Logo

std-proposals

Advanced search

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

From: Federico Kircheis <federico_at_[hidden]>
Date: Thu, 29 Jun 2023 16:58:10 +0200
On 29/06/2023 16.46, Frederick Virchanza Gotham via Std-Proposals 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.


Should following code trigger the proposed diagnostic?

void foo(const std::synchronized_value<MyClass>& v){
  v->SomeMethod();
}


void bar(){
   foo(g_obj.synchronize());
}

and what about member variables of classes as local objects?

struct foo{
   std::synchronized_value<MyClass> v;
};

void bar(){
  auto f = foo{g_obj.synchronize()}
  f.v->SomeMethod();
}

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