为什么在 unique_ptr 上使用 std::move 会破坏它?

Why does using std::move on a unique_ptr destroy it?

所以我对移动语义的理解是这段代码:

#include <iostream>
#include <memory>

class hello {
public:
    ~hello() {
        std::cout << "destroyed" << std::endl;
    }
    hello() {
        std::cout << "constructred" << std::endl;
    }
};


void takecontrol(std::unique_ptr<hello>&& ptr) {

    ptr.release();
}

int main()
{
    auto ptr = std::make_unique<hello>();


}

应该造成内存泄漏并且只打印 "constructed."

然而当 运行 (http://cpp.sh/2upqq) 它破坏了对象!

对我来说,它似乎应该在 takecontrol 中移入 ptr 然后发布然后不删除所以它不应该被销毁。

我错过了什么?

如果你把这个函数的调用添加到main

int main()
{
    auto ptr = std::make_unique<hello>();

    takecontrol(std::move(ptr));
}

你会得到你期望的行为。