Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why local variables in the lambda object are const?

Tags:

c++

lambda

c++14

Following code doesn't compile. Because pt has type of const std::packaged_task<void()>> and operator() is not const.

auto packagedTask = std::packaged_task<void()>>([]{});
auto future = packagedTask.get_future();
auto function = [pt = std::move(packagedTask)]{ (*pt)(); });

Here is workaround:

auto packagedTask = std::make_shared<std::packaged_task<void()>>([]{});
auto future = packagedTask->get_future();
auto function = [pt = std::move(packagedTask)]{ (*pt)(); });

Why local variables in the lambda object are const? I want to make first code work without overheads to workarounds. What is best practice to solve the issue?

like image 394
Viktor Avatar asked Aug 15 '26 14:08

Viktor


1 Answers

Unless the lambda is marked as mutable, the generated lambda::operator() will be qualified as const. Marking your lambda as mutable will prevent this behavior:

auto function = [pt = std::move(packagedTask)]() mutable { (*pt)(); });

Why local variables in the lambda object are const?

Local variables in closures generated by lambda expressions are not const. It's the generated lambda::operator() that's qualified as const. A better question might be "Why is a lambda's operator() implicitly const?

It's because const is a better default than mutable. Mutability introduces complexity. Immutability makes code easier to reason about.

const should be the language-wide default, but that's impossible to change due to retrocompatibility. Since lambdas were a brand new feature, the committee decided to go with const by-default and opt-in mutability.

like image 161
Vittorio Romeo Avatar answered Aug 17 '26 04:08

Vittorio Romeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!