mercredi 26 juillet 2017

C++11: The mutable lambda doesn't seem to change the variable?

I've got a quick test below:

#include<iostream>
using namespace std;
int main(){
    int i=2;
    auto f=[=]()mutable{++i;};
    f();
    f();
    cout<<i<<endl;
    return 0;
}

But the result it still prints "2". Why i is not modified inside a mutable lambda? I'm using clang --std=c++1z.

Thanks!

Aucun commentaire:

Enregistrer un commentaire