mardi 18 décembre 2018

Is it legal to assign to std::function that was moved

Is the following code legal:

std::function<void()> CreateFunction(int i, std::function<void()> previous_f) {
   return [i,previous_f] { 
     std::cout << i << std::endl;
     previous_f();
   };
}


int main()
{
  std::function<void()> f = []{};
  for(int i=0;i<3;++i) {
    f = CreateFunction(i, f);
  }
  f();
}

It compiles and runs as expected - http://cpp.sh/2smb3, but I am concerned that assigning to an f after f have been moved may invoke undefined behavior.

Aucun commentaire:

Enregistrer un commentaire