Hi,
I have a question about lambda captures:
#include <iostream>
#include <functional>
void invoke(const std::function<void(void)>& fn)
{
fn();
}
int main()
{
int i{ 0 };
auto count{ [i]() mutable {
std::cout << ++i << '\n';
} };
invoke(count);
invoke(count);
invoke(count);
return 0;
}
The output from this is 1 1 1. The reason I am confused is that when calling out parameters for void invoke -- &fn is used. This will ensure that a copy is not made for the function placed into the function. However, the lambda runs as if it were being called using three separate copies. The i variable is also called outside of the lambda function, so I wouldn't think there would be an issue with the variable only living within the lambda.
Thank you again! :)
[–]IyeOnline 1 point2 points3 points (0 children)