0
Follow
0
View

Why is a lambda expression value capture designed to be const

roz0803 注册会员
2023-02-27 16:21

In C++, Value Capture in a lambda expression can Capture variables either by Value or by Reference. For value passing, lambda expressions copy the value of the variable into the closure of the lambda expression and store the value in the closure for use in the lambda expression. The captured value in this way defaults to const.

This is because, when we capture a variable, the lambda expression may be called multiple times, with each call creating a new closure. If the captured value is not const, then modifying the value in the closure will result in uncertain results on subsequent calls to the lambda expression. If the captured value is const, it ensures that the value in the closure will not be modified, ensuring the reentrant of the closure.

Of course, if we want to modify captured values in a lambda expression, we can also do so by using the mutable keyword. In this case, the variable in the closure generated by the lambda expression is still const, but we can use the mutable keyword to signal that the lambda expression can change the value of the captured variable.

About the Author

Question Info

Publish Time
2023-02-27 16:21
Update Time
2023-02-27 16:21

Related Question

Istio异常值检测打破没有度量的路由

基于Multisim的调幅

arcgis,显示xy数据

如何使用路由策略修改isis的路由开销

如何使用Visual Studio代码为Reversi棋盘游戏编写代码?

QGIS插件数据源问题

Kotlin Smart Cast on when expression

starwind iscsi共享存储

Lisp——作为参数传入另一个函数并在Let中调用的函数

在kotlin中共享ListView内容