void do_something(std::function<void(void)> callback){
callback();
}
int main() {
int counter = 0;
while(counter < 10000000) {
do_something([&](){ std::cout << "by reference:" << counter << endl; })
do_something([=](){ std::cout << "by value:" << counter << endl; })
++counter;
}
}
As shown in the above code, will a new lambda be created in each loop? Will there be a difference with capturing a value or a reference?
I've been searching for some articles, but still not quite sure. I believe value-capturing lambdas need to be created multiple times, while reference-capturing ones probably don't.
If a lambda captured by reference can be reused, will the copying of the lambda still happen? For example, will the following two functions still make a difference?
void do_something2(std::function<void(void)> callback){
callback();
}
void do_something2(std::function<void(void)> & callback){
callback();
}
Aucun commentaire:
Enregistrer un commentaire