This is a example code I wrote to learn std::move and unique pointers . Can anyone explain why line2 crashes but line1 works ? What does moving do in lambda f2 in my test program , Why does line1 work even after a std::move ? This is just for my learning and not a homework problem , i am very new to C++14 and C++11
void test()
{
std::unique_ptr<int> p (new int(278));
auto f1 = [](std::unique_ptr<int> i){std::cout << *i << std::endl;};
auto f2 = [](std::unique_ptr<int> &&i){std::cout << *i << std::endl;};
f2(std::move(p));
std::cout << *p << std::end; //line 1
f1(std::move(p));
std::cout << *p << std::end; //line 2
}
int main(int argc, const char * argv[]) {
test();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire