bycopy capture with an initializer
Here's an example of how to use "bycopy capture with an initializer":
```cpp
#include <iostream>
variable used in lambda#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// Lambda that captures by copy
auto byCopy = [numbers = numbers_copy(numbers)]() mutable {
numbers.push_back(10);
};
byCopy();
// Output: 1 2 3 4 5 10
for (int num : numbers)
std::cout << num << " ";
std::cout << std::endl;
return 0;
}
```
In the example above, the `numbers_copy` function is used to create a copy of the `numbers` vector. This copy is then captured by the lambda expression `byCopy`. Any modifications made to the captured variable within the lambda will not affect the original `numbers` vector.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论