C++ Logo


Advanced search

Re: lambda, capture by const-ref

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 16 Sep 2021 19:42:25 -0700
On Wednesday, 15 September 2021 14:05:06 PDT Federico Kircheis via Std-
Discussion wrote:
> So, to sum it up, I would like see the possibility to add const to the
> lambda capture.
> Just this qualifier (leaving thus volatile out), and not the type (it
> can be done independently if we acknowledge it might be a good idea).

Hello Federico

Do you have a different use-case for this? Because you had a working, ideal
solution, then you created a problem and you're now looking for a solution to
that problem you created.

What's wrong with:

bool foo(data_and_mutex& d_m){
     const auto& value = get_value();
     return d_m.lock([&](data& d){
         bool res = d.str == value;
         return res;
     } );
You wrote:
> In this case, `foo` does not do much work, but we expanded a lot the
> scope of the variable returned by get_value.
Not in this case. And it's just a matter of adding more braces if you have 
further code and would like to terminate the lifetime extension as early as 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2021-09-16 21:42:32