mercredi 1 juillet 2015

Why is this std::unique_ptr being returned without a std::move statement [duplicate]

This question already has an answer here:

I am trying to familiarize my self with the std::unique_ptr and I understand that these pointers can only be moved. This is the code that I am trying out

struct foo
{
    int a;
};

std::unique_ptr<foo> GetPointer()
{
    std::unique_ptr<foo> f(new foo());
    return f;
}

int main()
{
    std::unique_ptr<foo> m = GetPointer();
}

I am using the flag -fno-elide-constructors to disable compiler optimization that reduces the copies made. My question is why is the f being returned ? It is an lvalue so I am assuming it cannot be moved. I was expecting to get an error because of return f and I was under the impression it should be return std::move(f)

Aucun commentaire:

Enregistrer un commentaire