lundi 4 juin 2018

Who has responsible for freeing resource moved by std::move?

Consider simple code below which crashes:

#include <iostream>

struct test 
{
    int *x;
    test(){ x = new int;}
    ~test(){delete x;}
};
int main() {
    test x;
    test y = std::move(x);
    return 0;
}

My question is that when object’s resources are moved by std::move, what will happen when its destructor is called as natural course of object going out of scope? Does that mean we should not call std::move for object on stack?

Aucun commentaire:

Enregistrer un commentaire